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 ???
mohammedokashaAsked:
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:
Hi!

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. :)

http://jakoch.de/2012/02/27/php-importsql-und-getqueriesfromsqlfile/

Regards,
     Tomas Helgi
0
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Julian HansenCommented:
This should do it - just change the connection parameters and the $filename variable
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");	

// CHECK FOR CONNECTION ERRORS 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

// CHANGE TO THE PATH OF YOUR FILE
$filename = "input.sql";
$file = file_get_contents($filename);
$lines = explode("\n", $file);
foreach($lines as $l) {
	$mysqli->query($l)
}
$mysqli->close();
?>

Open in new window

0

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
PHP

From novice to tech pro — start learning today.