views:

48

answers:

3

is the code given is executable in Windows system? as it seems to be Linux commands

   echo 'create database foo2' | mysql -uroot
    mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

EDITED I am executing the following code but I am not getting what I expect..

<?php 
mysql_connect('localhost','root','********');
echo 'create database pranav_chk'. | mysql -u root
    mysqldump --skip-triggers -u root pranav_test | mysql -u root pranav_chk;
 ?>
+2  A: 

The code uses three command-line features:

  • An echo command
  • Connecting commands via pipes (the | symbol)
  • The mysql command line client

All three are available and work identically (to the degree they're used here) on Windows and on Linux, so the code is in fact portable.

Edit: The code needs to be run in the shell, you can't just put it into a PHP script. You have to use exec(), something like this:

<?php
  exec("echo 'create database foo2' | mysql -uroot");
  exec("mysqldump --skip-triggers -uroot foo | mysql -uroot foo2");
?>

However, the mysql_connect() call before won't do you any good, because it's only valid for the PHP script. You'll have to do authentication via the command line directly. Alternatively, you could use it and mysql_create_db() to replace the first exec() line.

Michael Borgwardt
its Not Working for me... See the updated question for what i am executing..
OM The Eternity
@OM updated the answer
Michael Borgwardt
Thanks @Michael It worked
OM The Eternity
+1  A: 

It should work yes, assuming mysql and mysqldump are in the windows PATH variable. Although you should add a space between the -u and the root.

Obsidian
adding space will not Work...
OM The Eternity
+1  A: 

3 notes.

First, the database connection in the PHP script is not shared with the outside system command.

Second, you need to supply password to the command line.

Three, use the exec function to run your command.

For example:

<?php
$PASSWORD="YOURPASSWORD";
exec("echo 'create database foo2' | mysql -u root -p $PASSWORD");
?>

Not tested but thats the general idea.

zaf
Pls correct the answer by NOT seperating "-uroot" and "-p$password"
OM The Eternity
That is the correct way. http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html
zaf