-1

我有一个 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新手,知道为什么吗?

4

5 回答 5

1

3号线

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

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

我的假设是您的数据库名称 = "db" ,您的用户 = "user" 和用户的密码 = "password"

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

于 2013-09-11T04:11:09.007 回答
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(),因为如果未明确定义连接变量,它将不会返回值。

这种验证直接取自mysqli_connect() 文档示例。

于 2013-09-11T04:29:13.423 回答
0

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

于 2013-09-11T04:22:12.323 回答
0

Unknown MySQL server host

mysql找不到你的服务器

在这一行中,检查您的服务器

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

检查文档

于 2013-09-11T04:11:29.707 回答
-2
$con=mysqli_connect("server","db","user","password");//establish connection

你需要定义这些变量

DEFINE('SERVER', 'localhost');

DEFINE('db','whatever');

DEFINE('user','nickcage');

DEFINE('password','conair');
于 2013-09-11T04:12:30.660 回答