need php code that allow me to load .sql file to my database using php ??

i have test.sql file on my host, and  that file contain data like that:

INSERT INTO `x_world` VALUES (63855,'Saihaty',181,'T|N|T 1',22);
INSERT INTO `x_world` VALUES (63855,'Avs',181,'T|N|T 1',22);
INSERT INTO `x_world` VALUES (63855,'ERER',181,'T|N|T 1',22);

Open in new window

how i can load that data to my database using php ???
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomas Helgi JohannssonCommented:

Depending on whether you are using the old mysqli or PDO you could use either

mysql exec or pdo exec functions in combination with this solution that I found on the internet when I was doing some php mysql work. :)

     Tomas Helgi
Ray PaseurCommented:
If you trust the contents of the file, you can read the file with file(), then interate over the array elements with foreach() and use each element in a query.  This article shows how that might be done, and will also help you with the necessary conversion from MySQL to one of the other extensions.
Julian HansenCommented:
This should do it - just change the connection parameters and the $filename variable
$mysqli = new mysqli("localhost", "user", "password", "database");	

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());

$filename = "input.sql";
$file = file_get_contents($filename);
$lines = explode("\n", $file);
foreach($lines as $l) {

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.