This works when I run it from the mysql cli client but not when I run it through PDO, does anyone know why?
source is a command that allow you to execute list of SQL in mysql CLI,
but PDO is a database driver library (api) that compiled into PHP,
both are in different domain
Details of source :-
https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
http://dev.mysql.com/doc/refman/5.0/en/batch-mode.html
You can either :-
exec("/PATH/mysql < $file");
Or break each line of SQL (inside the file),
execute one-by-one each in PDO
Related
I am attempting to use the sqlite backup command through php.
This requires 3 statements
Opening the db sqlite3 testing.sqlite
Backup the DB .backup testing_backup.sqlite
Close sqlite .exit
The exec command doesn't like this as it hangs when the process stays open, I have tried running all three commands together using && to join them but this doesn't work either.
Can anyone help me run the 3 commands through php?
This is an attempt to create a backup file to solve a database locking issue.
The sqlite3 command-line shall can also receive command(s) as parameters.
Just execute the following command:
sqlite3 testing.sqlite ".backup testing_backup.sqlite"
I want to be able to dump a whole sqlite database to a file (database.sql) so as to use it as a backup. The problem is that exec() in PHP doesn't seem to run correctly the ".dump" command as it gives only "COMMIT;" as a result and not the whole text:
$db = new SQLite3('checks_db.db');
$results=$db->exec('.dump');
$content=$results->fetchArray();
If I run it from sqlite3 it gives both the schema and the insert insrtuctions with the data.
Perhaps I should also mention that the database has 777 permissions.
Can anyone write an example of the syntax of the '.dump' command in php-sqlite3?
The SQLite database does not have a .dump command.
The sqlite3 command-line shell has a .dump command.
To be able to use it, you would have to execute that tool; something like this:
exec('sqlite3 /some/where/checks_db.db .dump', $output);
The easiest way to make a backup would be to copy the database file itself, but using the shell's .backup command would be safer.
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
I am attempting to dumping a Microsoft SQL (MSSQL) database to a file similar to how i would dump from a MySQL database using php:
$cmd = "mysql --user=".$db_user." --password=".$db_password." --host=".$hostname." --database=".$database." --execute='SELECT * FROM ".$database." '> file.txt";
exec($cmd);
But i am trying to figure out the similarities in the command line as to how it is done above with MySQL
What is the proper command line syntax to execute this?
Thanks.
First, you need to write and test your BACKUP command in TSQL. Second, you need to execute it, either using sqlcmd.exe (Windows only) or by connecting directly to the database with whatever MSSQL drivers you use in PHP.
Since the SQL Server documentation is very complete and has numerous examples, you should be able to write the commands yourself. If you still have problems, please post what you have tried so far and exactly what you can't figure out.
Finally, you should always mention the version and edition of SQL Server, because it's often important to know when answering questions.
i have mysql file. it has .sql extension. how to run that "mysql" file using php script? how is the script should be?
the mysql file is already on the server, and i want to run this query using php file which is placed on the same place with mysql file...
You could do this
$query=file_get_contents($file);
$mysqli->multi_query($query);
mysqli::multi_query can do the job.
the mysql file is already on the server
dun use PHP ....
Linux ???
install mysql client, and run a shell like
mysql -u root -ppassword -P port_number < any.sql
Windows ???
still need to install mysql client, repeat from above
if you don't have ssh access to the server, and don't have exec right
read/prepare again the mysql file by breaking multiple queries then execute one-by-one