View Full Version : Populate DB from a PHP script
guvenck
08-10-2007, 04:09 PM
Hello,
This may belong to php section but I guess there could be people here who have the information I am looking for.
I have a MySQL dump generated by phpMyAdmin. Let's say > mydb.sql
I would like to populate my database from within a php script using this file. Is there a command to do that or should a special function be written?
Regards,
guelphdad
08-10-2007, 04:23 PM
why do you need to run it in a php script? couldn't you just write a shell script to do it and call the shell script from php if necessary?
guvenck
08-10-2007, 06:12 PM
Hi, it is for an installer script. I want to populate the database with core and demo data.
Any solution is welcome. You mean the system command?
hcamelion
08-13-2007, 12:15 PM
You should create an install file that does all the work. Just run each sql command one at a time as you would always do in php. I have attached a file that shows a basic install file. Look it over maybe it will give you some ideas. Or you can just do it via php's system() or exec() commands like this:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = 'test';
$file ='/tmp/file_restore.sql';
if ($dbpass != '') {
$cmd = '/usr/bin/mysql -h '.$dbhost.' -u '.$dbuser.' -p
'.$dbpass.' < '.$file;
exec($cmd,$out,$retval);
} else {
$cmd = '/usr/bin/mysql -h '.$dbhost.' -u '.$dbuser.' < '.$file;
exec($cmd,$out,$retval);
}
I got that from somewhere on the web so it isnt tested but should work or at least get you started. If it doesnt work your host could disallow system functions to take place.
That is useful if it is only for doing something like refreshing a database back to its original state. I use something similiar for a demo of the software I got that install file from which gets messed with by tons of people.
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.