473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\project s\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('localho st','root','',' devbb_testing', 3306);
$result = $db->query('\. C:\data\project s\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n". $db->error.":".$d b->errorno;
}
?>

Full source:

<?php

$db = new mysqli('localho st','root','',' devbb_testing', 3306);
$result = $db->query('\. C:\data\project s\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n". $db->error.":".$d b->errorno;
}
?>
Jul 23 '08 #1
2 3058
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\project s\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('localho st','root','',' devbb_testing', 3306);
$result = $db->query('\. C:\data\project s\forums\src/../tests_common/
tableSnapshots/1_users.sql');
if($result === false)
{
echo "failed\n". $db->error.":".$d b->errorno;}

?>

Full source:

<?php

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

?>
Jul 24 '08 #2
On Jul 24, 5:38*am, petersprc <peters...@gmai l.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_qu ery).

Taras
Jul 24 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1711
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 Development" by Luke Welling and Laura Thomson. I've set up an Apache Server v1.3.31. I've also installed MySQL V5.0.13 and PHP v 5.0.5. I've gone through the installations several times. I'm reasonably confident about the Apache Server but I have a problem with the interaction of the PHP and MYSql....
0
1598
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 opted for version 5.1.4. The remainder of my setup is as follows php 5.1.4 mysql 4.1.10a-2.RHEL4.1 httpd-2.0.52-12.2.ent Redhat Enterprise Linux 4 To be able to configure and compile the source of php to work with
12
4765
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', 'localhost'); define('DB_USER', 'ajaxuser'); define('DB_PASSWORD', 'practical'); define('DB_DATABASE', 'ajax'); // connect to the database $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
2
2768
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 new release, and restart Apache! Usually it goes off without a hitch, but I noticed that phpMyAdmin was not able to utilize the MySQLi library (which was working with my last 5.1.x release). As I was looking through php_info(), I noticed that MySQLi didn't even load. However, when I restarted...
13
3717
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 how to carry over a connection between methods as I am new to OOP? Thanks... Example; ======================================== <?php // webpage $newsignon = new newuser(); logon();
21
7280
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 classes, and decided to give them a go. Here's what I have to far: <?php class sql_db extends mysqli { var $connection = false;
2
3213
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 object to my object. The problem is, with code below I must call mysqli->connect() each time I call class methods. How do I create an connection for hole object, so methods can do queries without connect each time? Best Regards, Michael
221
367718
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 wouldn’t. In situations where your PHP application needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
0
1124
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 xp. $mysqli = new mysqli( "localhost", "user", "password", "database" ); Where localhost, user, password, and database where replaced with the appropriate values. I left the host parameter as "localhost" because that's what I wanted, however that did not work.
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.