I'm trying to connect to a database using a script by shell_exec
, but I'm getting:
mysqli::mysqli(): (HY000/2002): No such file or directory.
When using the script without shell_exec
(example: by submit button) it works perfectly.
On the caller php file:
shell_exec('php /Users/Desktop/save.php param')
Called php file:
$a=$argv[1];
saveonDB($a);
I really don't know why isn't working when calling the function by shell_exec
. The parameters are being passed correctly.
Thanks
UPDATE:
I made the example simpler:
connection.php:
function connect()
{
$servername = "localhost";
$username = "root";
$password = "root";
$dbname= "db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "connected";
}
}
connect();
caller.php:
echo exec ("php /Users/Desktop/connection.php");
When executing the connection.php directly, it says:
"connected"
When executing from caller.php it stays:
"Connection failed: No such file or directory"