PHP MySQL expects mysqli boolean given

前端 未结 5 923
一向
一向 2021-01-29 07:38

I have a php script that changes a database when the page is requested. Here is the php:



        
相关标签:
5条回答
  • 2021-01-29 08:19

    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

    0 讨论(0)
  • 2021-01-29 08:20

    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

    0 讨论(0)
  • 2021-01-29 08:22

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

    0 讨论(0)
  • 2021-01-29 08:31

    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.

    0 讨论(0)
  • 2021-01-29 08:38
    $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');
    
    0 讨论(0)
提交回复
热议问题