Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
May 24, 2012, 11:55:24 AM

Pages: [1]   Go Down
  Print  
Author Topic: mysql import via command line using PHP  (Read 2434 times)
trnitz
Newbie
*
Offline Offline

Posts: 3


« on: July 26, 2011, 06:44:04 AM »

I'm trying to import a Wordpress database via the command line, using PHP.

Yes, I know I can use the cPanel to do it via the phpMyAdmin (which is what I just did). 

I would like to have a simple php file that I can import the database without having to log into cPanel, as I'm probably going to be doing this often for testing purposes on a production Wordpress blog for a customer.

What I have in the php file:
Code:
<?php system('mysql -h localhost -u MyUserName -p MyPassword -d MyDatabase < MyDumpFile;'); ?>

The only output I get is the same as if I entered 'mysql -?` from the command line.

I have verified that the username, password, database, and dump file names are all correct.  When I changed it to a mysqldump command (with appropriate syntax changes), it worked fine.


I figure there is something I'm missing, but can't figure it out.
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5215



« Reply #1 on: July 26, 2011, 06:54:24 PM »

Try using an absolute path (starting with /) for the file you're importing. Who knows what directory you're starting in with the system() call, or what your environment settings are.

If what you want to do is load up a database from an .sql backup, without necessarily using the mysql command, it would be possible to parse the .sql file and break it up into individual SQL statements that could be fed to something like mysql_query(). I think that a lot of PHP/MySQL applications do that to do their initial database load.
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
trnitz
Newbie
*
Offline Offline

Posts: 3


« Reply #2 on: July 27, 2011, 03:48:10 AM »

Try using an absolute path (starting with /) for the file you're importing.

That was it! 

Thank you for your help.
Logged
smartkathy
Trekkie
**
Offline Offline

Posts: 17


« Reply #3 on: January 04, 2012, 12:00:18 AM »

@Phil

Thank you for your help. This is very useful information for all.
Cheers.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: