By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,274 Members | 2,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,274 IT Pros & Developers. It's quick & easy.

using mysqli to run 'source' command in PHP

P: n/a
Hi everyone,

I'm trying to run a number of commands stored within a sql file from
within php using mysqli::query. The syntax I'm using is:

source C:\data\projects\forum
s\src/../tests_common/tableSnapshots/1_users.sql

The command works fine from the mysql command line client, but when I
try to run this with the mysqli query function, I get the following
error (echoed from the error property of the db object):

You have an error in your SQL syntax; check the manual that
corresponds to your
MySQL server version for the right syntax to use near '\. C:\data
\projects\forum
s\src/../tests_common/tableSnapshots/1_users.sql' at line 1

Does anyone know if you can run the source command from within php? If
not, does anyone have any suggestions on how best to run a SQL file
created using MySQL Administrator from within PHP?

Thanks

Taras

$db = new mysqli('localhost','root','','devbb_testing',3306) ;
$result = $db->query('\. C:\data\projects\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n".$db->error.":".$db->errorno;
}
?>

Full source:

<?php

$db = new mysqli('localhost','root','','devbb_testing',3306) ;
$result = $db->query('\. C:\data\projects\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n".$db->error.":".$db->errorno;
}
?>
Jul 23 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Unfortunately the source command is implemented in the command-line
client and is not in the MySQL DML.

You can instead execute the mysql command from php using exec. The
command might be: mysql -e 'source c:\script.sql'

Or you could also parse the file and execute each statement separately
using the API.

Regards,

John Peters

On Jul 23, 9:34 am, Taras_96 <taras...@gmail.comwrote:
Hi everyone,

I'm trying to run a number of commands stored within a sql file from
within php using mysqli::query. The syntax I'm using is:

source C:\data\projects\forum
s\src/../tests_common/tableSnapshots/1_users.sql

The command works fine from the mysql command line client, but when I
try to run this with the mysqli query function, I get the following
error (echoed from the error property of the db object):

You have an error in your SQL syntax; check the manual that
corresponds to your
MySQL server version for the right syntax to use near '\. C:\data
\projects\forum
s\src/../tests_common/tableSnapshots/1_users.sql' at line 1

Does anyone know if you can run the source command from within php? If
not, does anyone have any suggestions on how best to run a SQL file
created using MySQL Administrator from within PHP?

Thanks

Taras

$db = new mysqli('localhost','root','','devbb_testing',3306) ;
$result = $db->query('\. C:\data\projects\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n".$db->error.":".$db->errorno;}

?>

Full source:

<?php

$db = new mysqli('localhost','root','','devbb_testing',3306) ;
$result = $db->query('\. C:\data\projects\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n".$db->error.":".$db->errorno;}

?>
Jul 24 '08 #2

P: n/a
On Jul 24, 5:38*am, petersprc <peters...@gmail.comwrote:
Hi,

Unfortunately the source command is implemented in the command-line
client and is not in the MySQL DML.

You can instead execute the mysql command from php using exec. The
command might be: mysql -e 'source c:\script.sql'

Or you could also parse the file and execute each statement separately
using the API.

Regards,

John Peters
Hi John,

I'm trying to avoid exec for two reasons:
1: lots of configurations will disable the function for security
reasons
2: lack of error feedback (eg; file not found, syntax error)

Luckily I've found a mysqli function that executes multiple sql
statements (parsing the SQL file would have been a nightmare as
queries span multiple lines). Given I can only use it for a MySQL
database, but that's something I'm willing to accept (the function is
called mysqli_multi_query).

Taras
Jul 24 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.