Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import .sql file in mysql databse with php

Tags:

php

mysqli

I'm trying to import .sql file into mysql database using php but I can't

Am using like this code:

<?php

$sql = mysqli_connect('localhost', 'root', '123123', '');
mysqli_query($sql, "USE vmail");
mysqli_query($sql, "source insert.sql");
mysqli_close($sql);

echo ("Done")


?>

How can I import it??

thnx.

like image 373
deounix Avatar asked Dec 07 '25 03:12

deounix


1 Answers

Try this code. You have to read the sql file first and than execute:

eg:

$sqlSource = file_get_contents('insert.sql');

mysqli_multi_query($sql,$sqlSource);
like image 92
teo Avatar answered Dec 08 '25 17:12

teo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!