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

Executing a SQL script file from PHP

P: 25
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
I'm using a version of MySQL that doesn't support stored procedures, so I'm looking for ways to get around this problem.
Sep 23 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 5K+
P: 5,058

You could try executing that exact line using exec.

You could also try the mysqli_multi_query method.
Sep 23 '08 #2

P: 25
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

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?
Sep 23 '08 #3

P: 25
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!!!
Sep 23 '08 #4

Expert 5K+
P: 5,058
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.
Sep 23 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.