我试图通过cron job连接到我的数据库。但是我一直收到错误消息。经过许多令人沮丧的时间后,我在这里寻求帮助。
我的cron脚本php文件:
<?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);
?>
这是我收到的错误:
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
有任何帮助吗?谢谢!
答案 0 :(得分:1)
你引用了常量,这使得它们成为字符串。
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DB_DATABASE);
答案 1 :(得分:1)
您应该使用以下引号编写constants
。否则它们是通常的字符串。
$conn = mysqli_connect(HOST, USERNAME, PASSWORD,DB_DATABASE);