如何从PHP调用MySQL交互式客户端?

Hom*_*er6 3 php mysql shell system

我想要的

`mysql -uroot`;
Run Code Online (Sandbox Code Playgroud)

进行MySQL交互式客户端就像执行一样

$ mysql -uroot 
Run Code Online (Sandbox Code Playgroud)

从shell做的.

如果PHP脚本在之后(或之前)存在,那也没关系,但我需要它来调用MySQL客户端.

我尝试过使用proc_open(),当然还有system(),exec()和passthru().想知道是否有人有任何提示.

Wri*_*ken 6

新解决方案:

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

老一:

保存标签完成(如果你用fread而不是使用fgets读出字节,你可能会在那里得到它),这会让你继续前进,还有许多需要调整:

<?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);
Run Code Online (Sandbox Code Playgroud)