PHP MySQL期望给出mysqli布尔值

时间:2013-09-11 04:06:57

标签: php mysql mysqli boolean

我有一个php脚本,可以在请求页面时更改数据库。这是php:

<?php
date_default_timezone_set('EST');//set time zone
$con=mysqli_connect("baukin.fatcowmysql.com","sas","***pass***","dbname");//establish connection
// Check connection
if (mysqli_connect_errno())//ping database to check connection
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();//error message
  }
else
{
echo "Successfully updated database";//success message
}
mysqli_query($con,"UPDATE dryers SET STATE='$_GET[state]' WHERE MACHINEID ='$_GET[machine_id]' ");//update state

mysqli_query($con,"UPDATE dryers SET UPDATETIME=date('m/d/y H:i:s') WHERE MACHINEID = '$_GET[machine_id]' ");//update time

mysqli_close($con);//close connection
?>

我收到此错误:

    Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'server' (0) in /home/content/57/10410357/html/updatedatabase.php on line 3
Failed to connect to MySQL: Unknown MySQL server host 'server' (0)
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/content/57/10410357/html/updatedatabase.php on line 13

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/content/57/10410357/html/updatedatabase.php on line 15

Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/content/57/10410357/html/updatedatabase.php on line 17

我是php新手,知道为什么?

5 个答案:

答案 0 :(得分:1)

第3行

$con=mysqli_connect("server","db","user","password");//establish connection

将“服务器”更改为您的主机地址或“localhost”

我的假设是您的数据库名称=“db”,您的用户=“用户”和用户的密码=“密码”

如果不只是更改为正确的数据库名称,用户和密码

答案 1 :(得分:1)

发生此错误是因为$con未正确初始化(可能是因为,正如许多服务器所做的那样,MySQL仅限于本地连接)。这就是你在mysqli_connect()之后得到错误的原因。

如果无法连接到MySQL,则应该终止脚本或显示错误消息,而不尝试查询空对象。

另一种选择:

if (!$con) {
    echo "Error connecting to MySQL: " . mysql_connect_error();
    die; // You can kill the script here
}

另一个:

if (!$con) { // Error connecting
    echo "Error connecting to Mysql: " . mysql_connect_error();
    // Another message here if you want
}
else { // Connection ok
    mysqli_query( /* bla bla bla */ );
    // ...
}

请注意,我正在验证$con值(如果null而不是mysqli_connection_errno()),而不是{{1}},因为如果连接变量未明确定义,它将不会返回值

此类验证直接来自mysqli_connect() documentation示例。

答案 2 :(得分:0)

Unknown MySQL server host

Mysql无法找到您的服务器

在此行中,检查您的服务器

$con=mysqli_connect("server","db","user","password")

Check the documentation

答案 3 :(得分:0)

将服务器更改为localhost,即使它是您的服务器的名称。服务器始终是localhost。

答案 4 :(得分:-2)

$con=mysqli_connect("server","db","user","password");//establish connection

您需要定义这些变量

DEFINE('SERVER', 'localhost');

DEFINE('db','whatever');

DEFINE('user','nickcage');

DEFINE('password','conair');