Run mysql shell commands from PHP

Run mysql shell commands from PHP

am 08.05.2006 14:48:49 von apostolosl

Hi there.
I need to update a remote database from a local one. I use mysqldump in
my php page in order to create a file for each table, like this:

$command="mysqldump table_products > table_products.sql";
system($command);

This works perfect and it gives me a file with a drop-table,
create-table and all the insert values.

Then I need to connect to the remote server and run the
table_products.sql file.

The way I do that is by calling the following in my php page:

$command="mysql --host=$host --user=$user --password=$pass
--database=$db;";
system($command);

, where $host, $user etc are being set at the top of the page. This
works fine and I get connected to the remote database.

Now I need to run the following mysql command: source stb_products.sql

When I try this sequence from a command line, it works perfect. When I
call "mysql --host=$host --user=$user --password=$pass --database=$db;"
in command line I get the "mysql>" prompt, and the run the "source
table_products.sql" and the remote database gets updated.

When I do this through PHP the last command doesn't work.

I guess I am not calling 'source' from within a "mysql" prompt?

How should I do it?

Any other ideas for achieving the same result? I need to do everything
from a single PHP page that runs on a local machine.

Thanks in advance,
Asaq

Re: Run mysql shell commands from PHP

am 08.05.2006 14:58:47 von apostolosl

In case I have confused you, this my php code. Last step doesn't work,
all I get is a mysql information page...

PHP CODE--->

//DUMP DATA IN FILE
$command="mysqldump backcatalogue table_products > table_products.sql";
system($command);

//CONNECT TO REMOTE DATABASE
$command="mysql --host=$ohost --user=$ouser --password=$opass
--database=$odb;";
system($command);

//UPDATE REMOTE DATABASE FROM FILE
$command ="mysql source stb_products.sql;";
system("command ") ;

END PHP CODE -->

Re: Run mysql shell commands from PHP

am 08.05.2006 15:02:09 von apostolosl

//DUMP DATA IN FILE
$command="mysqldump backcatalogue table_products > table_products.sql";
system($command);

//CONNECT TO REMOTE DATABASE
$command="mysql --host=$ohost --user=$ouser --password=$opass
--database=$odb;";
system($command);

//UPDATE REMOTE DATABASE FROM FILE
$command ="mysql source table_products.sql;";
system("command ") ;

END PHP CODE -->

Re: Run mysql shell commands from PHP

am 09.05.2006 19:56:21 von unknown

Post removed (X-No-Archive: yes)

Re: Run mysql shell commands from PHP

am 10.05.2006 02:19:34 von nc

apostolosl@gmail.com wrote:
>
> I need to update a remote database from a local one. I use mysqldump in
> my php page in order to create a file for each table, like this:
>
> $command="mysqldump table_products > table_products.sql";
> system($command);
>
> This works perfect and it gives me a file with a drop-table,
> create-table and all the insert values.
>
> Then I need to connect to the remote server and run the
> table_products.sql file.
>
> The way I do that is by calling the following in my php page:
>
> $command="mysql --host=$host --user=$user --password=$pass
> --database=$db;";
> system($command);
>
> , where $host, $user etc are being set at the top of the page. This
> works fine and I get connected to the remote database.
>
> Now I need to run the following mysql command: source stb_products.sql

You can run ONE command that would both connect you to the remote
server and execute the SQL file. Something like this:

$command = "mysql --host=$host --user=$user --password=$pass " .
"--database=$db < stb_products.sql";
system($command);

Cheers,
NC