• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved CronJobs - Error on mysqli_connect

Hi there,

I'm having a big trouble trying to run a php script with the command mysqli_connect(), it always return error (doesn't say what error), i debugged and the mysqli_connect does not work.

What should I do to make this work?

thanks
 
Go to Tools&Settings > PHP Settings and check that mysqli module is enabled for your PHP handler.
 
This does not seem to be a Plesk issue, but a programming issue with PHP. Could you please post your code and what error the script returns exactly when you run mysqli_connect()?
 
This does not seem to be a Plesk issue, but a programming issue with PHP. Could you please post your code and what error the script returns exactly when you run mysqli_connect()?
Thats the thing... when I run the code by URL, it works, but by cron job it fails!

PHP:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', '********');
define('DB_PASSWORD', '**********');
define('DB_DATABASE', 'lojaweb_astra');

$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

this code is used across all website, only crons don't work!
 
If you do not see an error message, how did you know in your first post "i debugged and the mysqli_connect does not work"?
 
Please expand your PHP code similar to this to make the error visible that your mysqli-command returns:
Code:
        $_SESSION['conn'] = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE));
        if (($_SESSION['conn'] === false) || ($_SESSION['conn'] == '') || ($_SESSION['conn']->connect_errno != '') || ($_SESSION['conn']->connect_error != ''))
            msg('cannot login, connect error ' . $_SESSION['conn']->connect_errno . ' "' . $_SESSION['conn']->connect_error . '".' . "\r\n");
 
Please expand your PHP code similar to this to make the error visible that your mysqli-command returns:
Code:
        $_SESSION['conn'] = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE));
        if (($_SESSION['conn'] === false) || ($_SESSION['conn'] == '') || ($_SESSION['conn']->connect_errno != '') || ($_SESSION['conn']->connect_error != ''))
            msg('cannot login, connect error ' . $_SESSION['conn']->connect_errno . ' "' . $_SESSION['conn']->connect_error . '".' . "\r\n");

it does not return anything... Still only this:
"Task "/var/www/vhosts/lojaweb.pt/dev.lojaweb.pt/lojapd/system/crons/getpayments.php" completed with error in 0 seconds."

you can try yourself!

https://lojaweb.pt/crons/getpayments

The PHP is Working, it make a connection with database to retrieve credential for the script to run, so, is not the script, it's something else!
 
O.k., maybe I should have been clearer. With "... PHP code similar ..." I meant that you need to adapt the code snippet to your needs. For example, the "msg()" function is not a PHP function. Instead you could use "echo('...')" to output the result to the console or "error_log('...')" to write the output to your web server error_log file. I am sorry, but I cannot spend so much time to individually deliver code samples that perfectly match your individual situation. I thought that it would be clear from the sample how to retrieve the error code and message from mysqli.
 
Solved!

Don't know how or why, but the cron did not like to start with the config file! So, I added the connection to the cron script, and it works fine now!

thanks for the help!
 
Solved!

Don't know how or why, but the cron did not like to start with the config file! So, I added the connection to the cron script, and it works fine now!

thanks for the help!
Hi,
I have similar problems,

I have the same problem, I can't connect to the database.
I would like to know about your solution, the acript where you put it and the code.

if anyone has example code ( login.php and connect.php ) I would appreciate it.
my bank is working ok and with data to test.

thanks,
Carlos.
 
Back
Top