1

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.

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i am using multi_query() otherwise i had to fix path to mysql in windows for exec to work properly.
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.