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

时间:2011-07-20 22:29:20

标签: php mysql shell system

我想要

`mysql -uroot`;

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

一样
$ mysql -uroot 
来自shell的

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

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

2 个答案:

答案 0 :(得分:6)

新解决方案:

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

旧的:

保存选项卡完成(如果你用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);

答案 1 :(得分:0)

我想它确实有效,但它正在等待一些输入。尝试向它发送一些sql命令stdin。当然,由于反引号操作符不支持IO重映射,因此您需要更复杂的流程处理。