Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running MySQL *.sql files in PHP

I have two *.sql files that I use when creating a new web site database. The first file creates all the tables. The second file populates some default records. I would like to execute these files from PHP. I also use the Zend_Framework, if that will help accomplish this.

Additional Info

  1. I don't have console access
  2. I'm trying to automate site generation from within our application.

SOLUTION

Using shell_exec()...

$command = 'mysql'         . ' --host=' . $vals['db_host']         . ' --user=' . $vals['db_user']         . ' --password=' . $vals['db_pass']         . ' --database=' . $vals['db_name']         . ' --execute="SOURCE ' . $script_path ; $output1 = shell_exec($command . '/site_db.sql"'); $output2 = shell_exec($command . '/site_structure.sql"'); 

...I never did get useful output, but followed some suggestions on another thread and finally got it all working. I switch to the --option=value format for the commands and used --execute="SOURCE ..." instead of < to execute the file.

Also, I never got a good explanation of the difference between shell_exec() and exec().

like image 419
Sonny Avatar asked Oct 26 '10 20:10

Sonny


People also ask

How do I run a .SQL File in PHP?

You should run your . sql script in PHP by invoking the mysql tool, for instance with shell_exec() . I got this test working: $command = "mysql --user={$vals['db_user']} --password='{$vals['db_pass']}' " .

How do I run a .SQL File from MySQL command line?

use the MySQL command line client: mysql -h hostname -u user database < path/to/test. sql. Install the MySQL GUI tools and open your SQL file, then execute it.

How do I run a .SQL File in SQLyog?

Select Tools -> Execute SQL Script (Ctrl+Shift+Q).Select the file and press Execute. If SQLyog encounters an error it stops further execution and logs the query as well as the error message in sqlyog. err located in the SQLyog installation folder.


1 Answers

This question comes up from time to time. There's no good solution for running a .sql script directly from PHP. There are edge cases where statements common in a .sql script can't be executed as SQL statements. For example, the mysql tool has builtin commands that are not recognized by the MySQL Server, e.g. CONNECT, TEE, STATUS, and DELIMITER.

So I give +1 to @Ignacio Vazquez-Abrams's answer. You should run your .sql script in PHP by invoking the mysql tool, for instance with shell_exec().


I got this test working:

$command = "mysql --user={$vals['db_user']} --password='{$vals['db_pass']}' "  . "-h {$vals['db_host']} -D {$vals['db_name']} < {$script_path}";  $output = shell_exec($command . '/shellexec.sql'); 

See also my answers to these related questions:

  • Loading .sql files from within PHP
  • is it possible to call a sql script from a stored procedure in another sql script?
  • PHP: multiple SQL queries in one mysql_query statement
like image 63
Bill Karwin Avatar answered Oct 02 '22 19:10

Bill Karwin