Connecting Tech Pros Worldwide Forums | Help | Site Map

Executing a SQL script file from PHP

Newbie
 
Join Date: Sep 2008
Location: Washington DC
Posts: 25
#1: Sep 23 '08
I'm a beginner with PHP, so I apologize if I'm asking something that's a common knowledge.

I know how to execute a single MySQL command and process its output. But what I'd like to do is to pass a sequence of commands to MySQL and let it execute the entire script and only pass back any potential output of the script.

Kind of, like what this line does from shell:
Expand|Select|Wrap|Line Numbers
  1. mysql -u userName -p < /path/to/file.sql
  2.  
I'm using a version of MySQL that doesn't support stored procedures, so I'm looking for ways to get around this problem.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Sep 23 '08

re: Executing a SQL script file from PHP


Hi.

You could try executing that exact line using exec.

You could also try the mysqli_multi_query method.
Newbie
 
Join Date: Sep 2008
Location: Washington DC
Posts: 25
#3: Sep 23 '08

re: Executing a SQL script file from PHP


I looked into the mysqli_multi_query() method, and it's a part of the mysqli class, which has its own connect. The description says it needs
Quote:

Originally Posted by http://php.net/mysqli_multi_query


A link identifier returned by mysqli_connect() or mysqli_init()

Our code uses mysql_connect() to connect to database. Unless I change everything else, can I make mysqli_multi_query() work?
Newbie
 
Join Date: Sep 2008
Location: Washington DC
Posts: 25
#4: Sep 23 '08

re: Executing a SQL script file from PHP


I also found that:
In order to have these functions available, you must compile PHP with support for the mysqli extension.

And, of course, it requires MySQL version 4.1 or more. We are still on 4.0... Ugh!!!
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#5: Sep 23 '08

re: Executing a SQL script file from PHP


Ugh indeed. I feel your pain.
You should go yell at the people in charge of that... There must be some law against torturing developers with old software :P

To use the mysqli functions you need a mysqli connection. The old MySQL extension and the new Improved MySQL extension (mysqli) don't work together.

Unfortunately there is no equivalent in the old mysql extension, so you would be forced to either execute it via a shell command or to parse the file into individual statements and execute them one at a time.
Reply