Python equivalent os.execvp for PHP

I have a PHP command line program running. And I want to connect to mysql shell directly from PHP. I have done this before in Python using os.execvp. But I can’t get the same for working in PHP.

I tried the following functions:

  • system
  • Passthru
  • Exec
  • shell_exec

Example:

system('mysql -u root -pxxxx db_name'); 

But they all seem to be waiting for mysql to exit and return something. I really want PHP to start the mysql shell and then exit it. any ideas?

+4
source share
3 answers

If you want shell commands to be interactive, use:

 system("mysql -uroot -p db_name > `tty`"); 

This will work in most cases, but will break if you are not in the terminal.

+3
source

Provide MySQL a script to run a separable script from PHP:

 system('mysql -u root -pxxxx db_name < script.mysql'); 
0
source

In addition to what acrosman said, you can also use the -e switch to pass SQL from the command line.

 $sql = "....."; system("mysql -u root -pxxxx db_name -e \"$sql\""); 

In addition, I hope that you are not actually connecting to the database as root from a PHP application.

0
source

All Articles