Page 1 of 1

How to execute MySQL file through PHP

Posted: Thu Nov 13, 2003 10:03 am
by php_wiz_kid
What's the query(MySQL) or function(PHP) to execute querys' in an external SQL file?

Posted: Thu Nov 13, 2003 10:07 am
by twigletmac
How are the queries stored in the file?

Mac

Posted: Thu Nov 13, 2003 10:27 am
by php_wiz_kid
They're just stored one after the other, nothing else, maybe some comments. Here's an example:

DROP TABLE IF EXISTS `rp_country`;
CREATE TABLE `rp_country` (
`countryid` tinyint(2) unsigned NOT NULL auto_increment,
`country_symb` char(2) NOT NULL default '',
`country` varchar(27) NOT NULL default '',
PRIMARY KEY (`countryid`)
) TYPE=MyISAM AUTO_INCREMENT=249 ;

That's just something from a SQL file that PhpMyAdmin made. I know they do it, and I know theirs is really complicated. I thought there was a way simple way to do it.

Posted: Thu Nov 13, 2003 10:29 am
by twigletmac
Can't you just read the contents of the file as a string and then use mysql_query()? You could always break it up at the ;'s and do a bunch of mysql_query() calls so you can add some decent error handling.

Mac

Posted: Thu Nov 13, 2003 10:31 am
by php_wiz_kid
Yeah, I guess I could, but I thought there was some kind of function or query that would do it for you. I guess I was wrong. Thanks for the help.

Posted: Thu Nov 13, 2003 11:36 am
by BDKR
Why can't you just use
mysql < your_mysql_file
?

That will read your file and execute the query statements within it. If the file is kicked out from phpMyAdmin, which is just acting as a front end for mysqldump anyways, then it should also have a command in there like...
use your_database;
... so it knows where to write this information (which database to execute these queries against). If it doesn't, you could type in the commmand as...
mysql your_database < your_mysql_file

When you get right down to it, the mysql client is rather powerful and flexible. Look at the below.
mysql -h 192.168.0.75 -u you -p -P 4417 your_database < your_mysql_file
You can find the information about it (as well as almost everything else you will need) at mysql.com. Also, those switches work with a good number of the other programs in the bin directory.

For the most part, there is no need to write code for this. Just be glad you haven't had to write scripts to parse the output from 3.23.xx binlog files. :evil:

Hope that helps,
BDKR