I think that the issue is not that you not get the output of the executed command, but which fails to find mysql. Using exec you can get the return status of your command, where 0 means successful, and other values indicate an error.

$output = exec(output, $retval);
var_dump($output);
var_dump($retval);

If the $retval is 1 that would mean which doesn't find the mysql binary, and returns an empty line.

Answer from Zsobrák Krisztián on Stack Overflow
🌐
PHP
php.net › manual › en › function.shell-exec.php
PHP: shell_exec - Manual
This function can return null both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.
🌐
Reddit
reddit.com › r/phphelp › php shell_exec not working with pipes
r/PHPhelp on Reddit: php shell_exec not working with pipes
May 28, 2022 -

Hi all I'm trying to SvtAv1EncApp tools via web interface, I try to us both exec() shell_exec() with no success , BTW I print the command and run it into the shell directly and it's works perfectly fine.

$cmd2="/usr/local/bin/ffmpeg -loglevel -8 -i "$mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "$mp4" -map 0:v -map 1:a:0 -c:v copy $a '".$mpa."_.mkv' & ";

shell_exec( $cmd2 ) ;

Here is the text of echo $cmd2 output

/usr/local/bin/ffmpeg -loglevel -8 -i "FHD.mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "FHD.mp4" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k 'FHD_.mkv' &

=Update=

I kinda solve it , I don't know why but exec() successfully executed the script , I created new bash script from command line thanks to @xisonc suggesting nano /usr/local/sbin/av1c with this value

!/bin/sh

touch /var/www/vl/ffmpeg.log /usr/local/bin/ffmpeg -loglevel -8 -i "$1" -s 960x540 -strict -1 -f yuv4mpegpipe -
| /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log
| /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "$1" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k "$2"

On the php script i had this

$cmd2="/usr/local/sbin/av1c '$mp4' '$mpa"."_.mkv'   2>/dev/null >/dev/null & " ; 
exec( $cmd2 , $pid, $r )    ;   
var_dump(  $r )                ;

var_dump returns 0 , and since the bash script are silent I don't need to see the output , now I want to change it to wok on the background it's running on background now after adding 2>/dev/null >/dev/null & at the end of the command .

🌐
Reddit
reddit.com › r/phphelp › why won't exec() or shell_exec() receive the output from this command?
r/PHPhelp on Reddit: Why won't exec() or shell_exec() receive the output from this command?
February 7, 2024 -
$output = shell_exec('mysql -u admin -padmin1234 -e \'create database `somedatabase`\'');

echo $output;

exec('mysql -u admin -padmin1234 -e \'create database `somedatabase`\'',$output_array);

print_r($output_array);

Both of exec and shell_exec result in creating the somedatabase database in mysql. But neither of these commands seem to capture the output. I know this command gives output because I can run it manually on the command line and see it. Is mysql just a special case or something? Running a simple command like ls -l works correctly, I can receive that output of that in PHP but cant for mysql for some reason.

Edit: Figured it out. The answer for me was understanding that 2> on the terminal catches the errors. So by using 2>&1 at the end of the command tells the terminal to redirect errors to the standard output (of which shell_exec will catch) https://stackoverflow.com/questions/52477206/how-to-redirect-mysql-output-to-file

🌐
SitePoint
sitepoint.com › php
Execute shell command, no return value - PHP - SitePoint Forums | Web Development & Design Community
November 15, 2019 - Hello. I have a command that I write (in promt/shell) to get a list of “correct options” of a mispelled word. When I press enter it returns a bunch of words. Everything works good. Problem is when I do the same thing through PHP shell_exec(‘my command’) it returns nothing.
Find elsewhere
🌐
PHP Freaks
forums.phpfreaks.com › web server administration › php installation and configuration
exec() does not always show output - PHP Installation and Configuration - PHP Freaks
July 17, 2020 - Hello, I can't get exec() working with a binary, but it works with whoami, the binary also outputs on a plain linux machine. I have error reporting E_ALL enabled but i can't find anything usefull in the logs. What can i do? Thanks for helping. See pic for the output of the script, whoami works bu...
🌐
Stack Overflow
stackoverflow.com › questions › 72654231 › php-shell-exec-exec-not-returning-all-output
PHP shell_exec() / exec() not returning all output - Stack Overflow
June 17, 2022 - Running a NodeJS script returning a long JSON string, both shell_exec() and exec() only seem to return the first approx. 72'000 chars (number slightly changes if JSON content changes, maybe due to UTF8 special chars). Are there any limitations I am not aware of? ... Works fine for me when I run echo strlen(shell_exec("printf -- '-%0.s' {1..800000} 2>&1")) . PHP_EOL; it outputs back the 800000 which I told it to repeat the dash character.
🌐
Linux Tip
linuxscrew.com › home › programming › php › php shell_exec function: how to use it [with examples]
PHP shell_exec Function: How to Use It [With Examples]
February 23, 2021 - This can be fixed by appending 2>&1 to the end of the command: $fileList = shell_exec('ls -la 2>&1'); echo "<pre>$fileList</pre>"; 2>&1 tells the shell to redirect stderr output to the current stdout – which resolves the issue.
🌐
PHPBuilder
board.phpbuilder.com › d › 10255923-shell-exec-not-returning-anything
shell_exec not returning anything? - PHPBuilder Forums
September 29, 2003 - mm, bit basic maybe but its got me stumped 🙁 trying to call a Perl script that should return some text by using: $output = shell_exec("./cgi/function 98")...