0

I try to run another PHP script through exec(), which works without the line $conn = new mysqli($db['host'],$db['uid'],$db['pwd'], $db['db']); (creates log file) but doesn't work with it (no log file). In the browser db.php works fine with the connection line.

exec.php

<?php
$processid = exec('php db.php 2>&1 &', $output);
echo $processid;
print_r($output);
?>

=> Fatal error: Class 'mysqli' not found in db.php

db.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ob_start();

$db = array(
    'host' => 'localhost',
    'db' => 'db',
    'uid' => 'user',
    'pwd' => 'pwd'
    );

$conn = new mysqli($db['host'],$db['uid'],$db['pwd'], $db['db']); //Tested with and without
// // Check connection
// if ($conn->connect_error) {
//     die("Connection failed: " . $conn->connect_error);
// }
// $con = $conn;

echo "Test";

$htmlStr = ob_get_contents();
ob_end_clean(); 
file_put_contents('testdblog.txt', $htmlStr);

?>
Scripter
  • 558
  • 2
  • 8
  • 20
  • Is it because the extension is missing? Why don't you enable error reporting and check the error you get? – Dharman Aug 08 '21 at 20:40
  • 1
    First thing, remove `>/dev/null` and see if the command produces any output – Dharman Aug 08 '21 at 20:43
  • @Dharman db.php works fine if I browse to the page (even with actual queries). It seems that the exec() makes mysqli not found, but it is in phpinfo. ouput: Fatal error: Class 'mysqli' not found in db.php – Scripter Aug 08 '21 at 21:03
  • Maybe `exec('php db.php 2>&1 &', $output);` php here referes to another php version on the server that doesn't have `mysqli` extension. – Rain Aug 08 '21 at 21:21
  • 1
    The version you use from web and the version used in CLI is different. You need to enable mysqli extension for the CLI version too – Dharman Aug 08 '21 at 21:26
  • Thanks. That wasn't possible on my shared hosting, but `exec('/opt/plesk/php/7.2/bin/php db.php 2>&1 &', $output);` worked! – Scripter Aug 09 '21 at 20:53

0 Answers0