SQlite and PHP - Console issue - php

On Windows Server 2008 R2, I am trying to set up a scheduled task to automatically run a PHP script which then adjusts the SQlite database. I am using SQlite as I need to use a database file. Whenever I run the script, it returns this error:
But when I run it in the web browser, it works perfectly.
My code is:
$db = new PDO("sqlite:server_status.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM stats";
$results = $db->query($sql);
foreach($results as $row) {
print_r($row);
}

I'm no expert with PDO or SQLITE but looking at your console output it seems that your current working directory is c:/windows/system32 but that is not your web root.
Therefore PDO will be looking for your database file at: c:/windows/system32/server_status.db which i presume is not the correct path.
Either define your sqlite database location as a full absolute path starting with C:\, or change your current directory to the folder containing the sqlite database file.

Related

What is causing a white screen when calling mysql from php/localhost?

Running windows 10, php 8.1.13, community mysql 8.0, localhost:8000 on mozilla firefox.
I'm new to web development, I'm trying to use mysql with php to maintain a user database for a website. I test my development on a php localhost run from the windows command prompt (php -S localhost:8000 -t ) and then enter localhost:8000 as the URL in firefox. I've gotten lots of generic php code to run fine on localhost in this manner.
After downloading mysql, I'm able to successfully use procedural mysqli database instructions through php on the windows php shell. Through the php shell, I've created a mysql database with php, added simple entries with php, and then later retrieved them, so php is playing nicely with mysql.
Php is playing nicely with mozilla/localhost, and php shell is playing nicely with mysql, but when I try to combine the two processes (manipulate mysql database through a php server run as localhost) I just get a blank, white screen on firefox with no errors in the console or the terminal window.
The problem seems to happen almost immediately (this exact code executes fine (returns "success!") from the php shell window):
echo "test1";
$conn = mysqli_connect("localhost", $username, $password);
echo "test2";
if ($conn) 
{
echo "success!";
}
mysqli_close($conn);
echo "test3";
conn appears to be declared OK. If I comment out the if statement then both test1, test2, and test3 echoes are visible, and I can close $conn. It's only when I actually try to DO anything with the connection (even this if statement) that the white screen appears.
Advice? Do I need to initialize the mysql server or something when run in this manner? Thanks!
I was expecting at minimum a relevant error message. The individual components work (A talks to B, B talks to C, so I expected A -> B -> C no problem.

Code problem: cPanel cron job for running php script to execute a stored procedure

I need to run a MySQL stored procedure daily. I do it manually now from Workbench. I would like to automate that process. Running it as a scheduled events within MySQL phpMyAdmin is not available to me.
GoDaddy offers a cron job command line within cPanel that can run a php script. I have researched everything I can but cannot figure out the right combination to make this work. Needless to say, I am new at this. The php code has been borrowed from online coding tutorials.
The only thing I am sure of as far as code is concerned is the call to a stored procedure function that I run from Workbench. The stored procedure takes some complicated queries and makes static tables that are accessed by users on the front end. And I do know how to set the cron job run times, no problem.
Could you please tell me what I need to change to make the call function work?
The cPanel cron job command line reads:
/usr/bin/php -q /home/USERNAME/public_html/cron/sp_run.php
The php script file reads:
<?php
$host = 'localhost';
$username = 'USERNAME';
$password = 'PASSWORD';
$dbname = 'DBNAME';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// assign the execute stored procedure to the variable
$sql = 'CALL DBNAME.sp_make_tables()';
// execute the stored procedure
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Error occurred:" . $e->getMessage());
}
?>
My shared server application versions are:
cPanel Version 78.0 (build 38)
Apache Version 2.4.41
PHP Version 5.6.40
MySQL Version 5.6.46-cll-lve
Architecture x86_64
Operating System linux
Thank you for whatever help you can offer.

PHP outputs from command line but not from the browser

I have a PHP file testphp.php with the following content:
<?php
echo system('mysql -u root -pMyPassword -e "SELECT VERSION();"');
?>
It outputs perfectly from the command line:
D:\>php testphp.php
VERSION()
5.5.24-log
5.5.24-log
D:\>
When I execute the same file via a web browser (http://localhost/testphp.php) , I see no output. Why?
There are lots of reasons for this:
You are using the command mysql which may not be in the path of the user that the web server runs as. When you run it from the command line, you are running it with your own user account. The web server runs under a different (restricted) account. You should provide the full path to the mysql binary. Type which mysql as your normal user from the prompt to find out what is the full path to the executable.
The command mysql may be restricted by your system configuration so that not all users can execute it.
Your PHP configuration on the web server prohibits the use of system().
You can get the same information by running this (assuming you have mysql configured for your php installation):
$con = new mysqli("localhost", "root", "MyPassword");
printf("%s", $con->server_info);

SQLite database is locked, but only for insert from PHP script run at command line

I have a PHP script that does the following in the order presented:
Connect to a MySQL database
Retrieve a single row of data
Close the MySQL database connection
Connect to a SQLite database on the local file system
Insert the row of data into the SQLite database
Close the SQLite database connection
I'm using PDO as the vehicle for both MySQL and SQLite. Here is the code for the insert:
$sqlite = new PDO('sqlite:activity.sqlite');
$sqlite->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$lite = <<<LITE
INSERT INTO Transactions (Date,Transactions,ActiveMembers,Amount)
VALUES
('$day',
'$trans',
'$active',
'$amount');
LITE;
try
{
$sqlite->exec($lite);
}
catch (PDOException $e)
{
die($e->getMessage());
}
(please forgive the usage of variables in the query, rather than a proper prepared statement; I removed the bound parameters to see if that was the problem)
Using other tools (SQLite Manager for Firefox, SQLite Database Browser for Windows), I am able to access the database and write to it.
This script is the only thing that touches this particular database. The permissions on the database file are 777. Nothing else has it, or its containing directory, open when the script runs.
When running the script (using php -f), the portion that tries to insert into the SQLite database creates the journal file, takes about 10-15 seconds, and then returns the error SQLSTATE[HY000]: General error: 5 database is locked.
So my question, then, is this:
Is there something about running a PHP script from command line that prevents interaction with a SQLite database? If so, what? If not, what could be the issue here?
I have come across the same problem. After using the fuser command and finding which process had created a sticky lock I knew the rogue:
[root]# fuser cms.db
cms.db: 4511
[root]# ps aux | grep 4511
nobody 4511 0.0 3.2 74560 25160 ? S Oct13 2:00 php-fpm
[root]# /etc/init.d/php-fpm restart
PHP-fpm was the guilty one so restarting the service did the trick. That must have been a bug in the PHP version I was running (5.4) and may still be.
Be sure to clear your connection to SQLite, i.e. after your code, set
$sqlite = null;
See http://php.net/manual/en/pdo.connections.php

Can I move MySQL database from Server A to Server B if all I have is FTP and MySQL user/pass for Server A?

A friend of mine wants to move his website to my Slice Host slice. The site uses a MySQL database. He gave me FTP login info, but nothing else. I have called and emailed his hosting company hoping that they would be able to give me access to PHPMyAdmin if it is installed. I have not received a response from them. Neither has my friend.
I can find the database username, password, name, etc. in the PHP files via FTP.
I tried uploading and running (in a browser) this PHP file:
$con = mysql_connect("mysql.address.com","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
system("mysqldump -uuser -ppassword databasename > dump.sql");
?>
...but it did not create a file named dump.sql on the server.
Any ideas on how I can accomplish my goal?
Ultimately, mysqldump isn't special. It communicates with the MySQL server just like any other client. You could write PHP code to dump each table to e.g., xml and then more code to reload them all. For most web apps, the database will just contain base tables, so you'll really only need:
SHOW DATABASES
SHOW TABLES
The above two will tell you all the tables you need to dump. You could also do a select from information_schema.tables.
SHOW CREATE TABLE «foo»
This will give you the SQL to recreate the table.
SELECT * FROM «foo»
This will (obviously) give you the data.
I'm guessing instead of writing this, you can probably find pre-existing code. phpMyAdmin has it, I believe, but that's quite a bit of complexity for just doing a quick dump...
If you have FTP and really want phpMyAdmin, why don't you install it then?
You could use mysql GUI tools using the username and password you found, but this may only work from the server (or a file on the server).
Instead of calling a system() function which you won't have access to (neither will you have mysqldump in most cases on shared hosting), use the "echo" command on an sql query, so:
$myQuery = mysql_query("show tables");
while($row = mysql_fetch_row($myQuery))
{
echo $row[0];
}
You could also test whether the ftp username/pass gives you shell access (ssh) and try running your command from the command line as suggested in the comments
I should add its been a while since I've done php, so my code might not be quite right but you get the idea :)

Categories