472,353 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

using mysqli to run 'source' command in PHP

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
2 2963
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Malcolm Wright | last post by:
Although I've been programming for over 30 years I am new to web development. As an introduction I've been using a book "PHP and MYSQL Web...
0
by: Reestit Mutton | last post by:
I'm trying to get the mysqli object-oriented syntax to work in php. I understand that I have to have at least version 5 of php - I've therefore...
12
by: davids58 | last post by:
trying to figure out how to use a mysql database with PHP. I ran the following code: <?php // defines database connection data define('DB_HOST',...
2
by: Curtis | last post by:
Hello everyone: Recently, I decided to upgrade to PHP 5.2.0. I have C:\php5 in the Windows XP PATH, so upgrading is quite painless; just unzip...
13
by: Schmidty | last post by:
If you do a page reload with $_SERVER will your program lose a mysqli connection upon the reload of the page? Would this code work? I need to know...
21
by: Daz | last post by:
Hi everyone. I am trying to create an extension of the mysqli class within PHP, and I am finding it quite difficult. I am fairly new to PHP...
2
by: Michael | last post by:
Hi, I try to use mysqli object instead of standard mysql functions. Is it ok to create mysqli object within my class or schould I pass mysqli...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you...
0
by: anonymous | last post by:
I had a tough time getting PHP to connect to MySQL using mysqli on XP. The following code from the php documentation on mysqli failed to work on...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.