You can put any valid SQL command into the mysql_query command so you
could write a PHP file that had a bunch of lines like these (after your
connect and such)

     mysql_query ( "ALTER TABLE foo ADD COLUMN bar int..." );
     mysql_query ( "CREATE TABLE foo2 (foo2ID int)" );

Better still to check that these changes have not been made already first
and then to check for errors after each command and do something
intelligent (like print an error message for example).  Try this

     mysql_query ( "SELECT bar FROM foo") or mysql_query ( "ALTER TABLE
foo ADD COLUMN bar int..." );

at minimum or expand the above to set some variables to say what it found
done already, what was missing and what it tried to create and the results
from that.  Suppose the mysql user does not have "CREATE TABLE"
permissions - if you printed the mysql_error () you'd see this problem
instantly - saving you many hours of debug time.

One last thought - if you have a standard LAMP application and your using
one mysql user to do all of this you might want to think about it.  You
typically do not want the mysql user to have "CREATE TABLE" permissions
and such.  You also don't want to hard code a user and password into the
application upgrade script (a evildoer might find these and use this
account to break in).  So you might want this PHP page to ask for a user
name and PW that has these permissions - avoid hard coding this to root,
perhaps there is another account the sys admin wants to use.

Good luck,

On Oct 10, 2006, at 7:39 AM, [EMAIL PROTECTED] wrote:

From: "Daevid Vincent" <[EMAIL PROTECTED]>
Date: October 9, 2006 6:01:16 PM PDT
To: <>
Subject: What's the PHP equivallent of "mysql mydb < somefile.sql"

>Currently I run an 'updater' script to run through a directory of .sql files
>using something like this in PHP:
>        $COMMAND = "mysql ".$OPTION['db_prefix'].$db." < ".$mydir.$filename;
>        system($COMMAND, $ret);
>What would be the equivallent way to to this in a PHP mysql_query(""); way?
>I see "LOAD", but that only works for data it seems.
>These scripts I use have ALTER statements and CREATE and all other types of
>SQL in them and almost no data actually.
>mySQL 5.0.15

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to