3 Votes Vote

PHP: multiple SQL queries in one mysql_query statement

Posted by topdog 284 days ago Questions| sql mysql query All

So I have an SQL dump file that needs to be loaded using mysql_query(). Unfortunately, it's not possible to execute multiple queries with it.

-> It cannot be assumed that the mysql command-line client (mysql --help) is installed -- for loading the SQL file directly

-> It cannot be assumed that the mysqli extension is installed

/* contents of dump.sql, including comments */
DELETE FROM t3 WHERE body = 'some text; with semicolons; scattered; throughout';
DELETE FROM t2 WHERE name = 'hello';
DELETE FROM t1 WHERE id = 1;

The explode() below won't work because some of the dump content's values contain semicolons.

$sql = explode(';', file_get_contents('dump.sql'));
foreach ($sql as $key => $val) {
    mysql_query($val);
}

What's the best way to load the SQL without modifying the dump file?

Discuss Bury


Who Voted for this Question