views:

78

answers:

3

I've got a site that requires manual creation of the database tables on install. At the moment they are saved (along with any initial data) in a collection of .sql files.

I've tried to auto-create using exec() with CLI MySQL and while it works on a few platforms it's fairly flakey and I don't really like doing it this way, plus it is hard to debug errors and is far from bulletproof (especially if the MySQL executable isn't in the system path).

Is there a better way of doing this? The MySQL query() command only allows one sql statement per query (which is the sticking point).

MySQLi I've heard may solve some of these issues but I am fairly invested in the original MySQL library but would be willing to switch provided it's stable, compatible and is commonly supported on a standard server build and is an improvement in general.

Failing this I'd probably be willing to do some sort of creation from a PHP array/data structure - which is arguably cleaner as it would be able to update tables to match the schema in situ. I am assuming this may be a problem that has already been solved, so any links to any example implementation with pro's/con's would be useful!

Thanks in advance for any insight.

A: 

You can run the schema creation/update commands via the standard mysql_* PHP functions. And if the query() command as you call it will allow only one statement, just call it many times.

I really don't get why do you require everything to be in the same call.

You should check for errors after each statement and take corrective actions if it fails (unless you are using InnoDB, in which case you can wrap all statements in a transaction and rollback if it fails.)

Vinko Vrsalovic
The problem is it is generated from .sql files which may contain multiple commands - the initial table creation then the row inserts as created by phpmyadmin. As for running mysql_query () multiple times it'll only run the first command in the file multiple times unless I split the file up programmatically and I shudder to think what that code would look like.
Meep3D
See here: http://stackoverflow.com/questions/147821/loading-sql-files-from-within-php
Vinko Vrsalovic
+1  A: 

Apparently you can pass 65536 as client flag when connecting to the datebase to allow multi queries, e.g. making use of ; in one SQL string.

You could also just read in the contents of the SQL files, explode by ; if necessary and run the queries inside a transaction to make sure all queries execute properly.

Another option would be to have a look at Phing and dbdeploy to manage databases.

Gordon
I'll look into those links. As for explode by ;, it'll choke if one of the inserts uses ; legitimatley and I think detecting when would be too messy to contemplate.
Meep3D
+1  A: 

If you're using this to migrate data between systems, consider using the LOAD DATA INFILE syntax (http://dev.mysql.com/doc/refman/5.1/en/load-data.html) after having used SELECT ... INTO OUTFILE (http://dev.mysql.com/doc/refman/5.1/en/select.html)

MightyE