0

I try to operate "php xxx.php" with proc_open, it works fine, but I have a problem when I try to interact with mysql, I can't get the response data of mysql, it is output directly to the shell

code:

<?php
$handle = proc_open('mysql -uroot -p', [
 ["pipe", "r"],
 ["pipe", "w"],
 ["pipe", "w"]
], $pipes);
$world = stream_get_contents($pipes[1]);
var_dump($world);

Even if I just execute "mysql", I can't get its output, it seems that its output pipe is redirected somewhere else, how can I make it work as I expected.

I tried "bash -c 'mysql -uroot -p'" but still didn't get the expected behavior

For ease of understanding, I have appended the output:

[root@myrokcy code]# php myfile.php
Enter password: (Handle blocking here manually, otherwise the script won't continue)
string(0) ""
[root@myrokcy code]# 

If I adjust the output to stream_get_contents($pipes[2]);

[root@myrokcy code]# php myfile.php
Enter password: (Handle blocking here manually, otherwise the script won't continue)
string(83) "ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
"
[root@myrokcy code]# 

I still can't catch "Enter password: ", it is output first and blocks my reading and writing. I want to try to read this content and automate the interaction. Obviously, I fail. It shouldn't be a permission issue, my test account is "root", system: rocky8.6, it's just that I haven't modified the default "php.ini".

I think I still need to emphasize that the current problem is that the output from "$pipes[1]" cannot be obtained correctly, ""Enter password: "" is forced to output to the shell, and it will block until you operate, "$ world = stream_get_contents($pipes[1])" will not be executed unless you do something to stop the blocking process

If I'm doing it right, is this a bug? if it is i will report it.

asked Feb 4, 2023 at 18:56
4
  • I wonder if the stderr pipe has something of interest in; maybe like a permissions issue Commented Feb 4, 2023 at 19:03
  • I have supplemented the content, please check it out, thank you Commented Feb 5, 2023 at 3:17
  • Why do you need to use proc_open for interacting with the database? Why not PDO or mysqli? Commented Feb 8, 2023 at 21:21
  • Because I am trying to do some automated initialization tasks, this problem does not only occur in the mysql command, the git command will also appear the phenomenon I described. Commented Feb 10, 2023 at 0:11

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.