Khurram Ijaz
Khurram Ijaz

Reputation: 1864

how to load sql dump using mysql_query in php

To install a cms i have a form in which i ask user to enter a database name he wants the script to make tables into. After that i have my only three cms tables.

i am trying to read that dump.sql file using the file = get_file_contents(dump.sql) and then mysql_query($file); but nothing is happening please suggest or help.

Upvotes: 1

Views: 1439

Answers (2)

Baran Sakallıoğlu
Baran Sakallıoğlu

Reputation: 98

It will be a tricky solution but you can explode the whole sql file from ";" characters if you dont have data containing ; characters in it. If so it will be hard to escape them first.

For example:

$lines = file_get_contents("your_sql_dump");
$lines = explode(";", $dumpSql);
foreach($lines as $line) {
    mysql_query($line, $conn);
}

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86386

mysql_query can not run multiple queries in once.

you can try mysqli:multi_query() function

OR

There is another way to do this with exec and mysql command.

You can execute the mysql command to store the tables in database using the exec function of php.

That will be something like this

$Command = "mysql -u {$username} -p{$password} {$database} < {$BackupFile}";
exec($Command);

Upvotes: 4

Related Questions