开发者

How can I invoke the MySQL interactive client from PHP?

开发者 https://www.devze.com 2023-03-21 16:13 出处:网络
I\'m trying to get `mysql -uroot`; to enter the MySQL interactive client just as executing $ mysql -uroot

I'm trying to get

`mysql -uroot`;

to enter the MySQL interactive client just as executing

$ mysql -uroot 

from the shell does.

It's okay if the PHP script exists after (or before), but I need it to invoke the MySQL client.

I've tried using proc_open() and of course system(), exec() and passthru(). Wondering if anyone has any tips.开发者_StackOverflow社区


New solution:

<?php
$descriptorspec = array(
   0 => STDIN,
   1 => STDOUT,
   2 => STDERR
);
$process = proc_open('mysql -uroot', $descriptorspec, $pipes);

Old one:

Save for tab completion (you could probably get it in there if you read out bytes with fread instead of using fgets), this gets you on your way, lots left to tweak:

<?php
$descriptorspec = array(
   0 => array("pty"),
   1 => array("pty"),
   2 => array("pty")
);
$process = proc_open('mysql -uroot', $descriptorspec, $pipes);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking(STDIN,0);
do {
   echo stream_get_contents($pipes[1]);
   echo stream_get_contents($pipes[2]);
   while($in = fgets(STDIN)) fwrite($pipes[0],$in);
} while (1);


I guess it does work, but it's waiting for some input. Try sending some sql commands to it's stdin. Of course, since the backtick operator doesn't support IO remapping, you'll need more complex process handling.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号