Reputation: 953
I'm trying to connect to my database via cron job. However i keep receiving an error messages. After many frustrating hours im posting here for help.
My cron script php file:
<?php
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect('HOST', 'USERNAME', 'PASSWORD','DB_DATABASE');
// Check connection
if (mysqli_connect_errno())
{
"Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check if server is alive
if (mysqli_ping($conn))
{
"Connection is ok!";
}
else
{
"Error: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
This is the error i received:
mysqli_connect(): (HY000/2005): Unknown MySQL server host 'HOST' (0)
mysqli_ping() expects parameter 1 to be mysqli
mysqli_error() expects parameter 1 to be mysqli
mysqli_close() expects parameter 1 to be mysqli
Any help? Thanks!
Upvotes: 0
Views: 281
Reputation: 413
You should write constants
out of quotes as following. Otherwise they are usual strings.
$conn = mysqli_connect(HOST, USERNAME, PASSWORD,DB_DATABASE);
Upvotes: 1
Reputation: 11375
You're quoting constants, which makes them strings.
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DB_DATABASE);
Upvotes: 1