PHP MySQL expects mysqli boolean given

£可爱£侵袭症+ 提交于 2021-02-05 12:22:09

问题


I have a php script that changes a database when the page is requested. Here is the 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
?>

I get this error:

    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

I am new to php, any idea why?


回答1:


That error occurs because $con is not being correctly initialized (probably because, as many servers do, MySQL is restricted to local connections only). That's why you get the error after mysqli_connect().

If you can't connect to MySQL, you should kill the script or show an error message, without trying to query an empty object.

One alternative:

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

Another one:

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 */ );
    // ...
}

Note that I'm verifying the $con value (if null or not) instead of mysqli_connection_errno(), because it won't return a value if the connection variable is not well defined.

This kind of verification is taken directly from mysqli_connect() documentation examples.




回答2:


Line 3

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

Change "server" to your host address or maybe "localhost"

my assumption is your database name = "db" , your user = "user" and user's password = "password"

if not just change into correct db name, user and password




回答3:


Unknown MySQL server host

Mysql cannot find your server

In this line, check your server

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

Check the documentation




回答4:


change server to localhost, even if it is the name of your server. the server is always the localhost.




回答5:


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

you need to define those variables

DEFINE('SERVER', 'localhost');

DEFINE('db','whatever');

DEFINE('user','nickcage');

DEFINE('password','conair');


来源:https://stackoverflow.com/questions/18732502/php-mysql-expects-mysqli-boolean-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!