Tutorial :Inserting database structure into MySQL with PHP?



I'm trying to find a way to insert a whole database structure into MySQL with PHP. Now, my main question is if there's any way to do this beyond just inserting the whole bloody thing line by line?

edit: By "insert structure", i mean add the database and the tables - no more.



$sql = file_get_contents('./some-database-schema-you-trust.sql');  mysql_query($sql);  // or mysqli_query(), $pdo->query(), etc.  

As long as each statement is terminated by a semicolon, just running it all in one shot should be fine.


You can always randomly generate SQL statements and hope your randomly generated statements are what you want.

You can build a object model and reverse that into a database structure (many ORM wrappers can do that)

Or you can just include your create script and run it.


AFAIK using php you can run queries just one by one. You can do it in just one shoot by using the mysql bin or if you need only to insert data you can use mysql LOAD DATA

EDIT: I almost forget, you can try to use PDO with mysql driver, I successful used it with multiple queries only with sqlite but i think it can work also with mysql driver.

I also found another post on SO about it, it about the performances of PDO.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »