RE: What's the PHP equivallent of "mysql mydb < somefile.sql"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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,
Frank

On Oct 10, 2006, at 7:39 AM, php-db-digest-help@xxxxxxxxxxxxx wrote:

From: "Daevid Vincent" <daevid@xxxxxxxxxx>
Date: October 9, 2006 6:01:16 PM PDT
To: <php-db@xxxxxxxxxxxxx>
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.
>http://dev.mysql.com/doc/refman/5.0/en/load-data.html
>
>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
>
>
>ÐÆ5ÏÐ
>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux