How to execute MySQL file through PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

How to execute MySQL file through PHP

Post by php_wiz_kid »

What's the query(MySQL) or function(PHP) to execute querys' in an external SQL file?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

How are the queries stored in the file?

Mac
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Post Reply