Testing php / mysqli connection

后端 未结 3 2032
长发绾君心
长发绾君心 2020-12-10 16:18

I am looking for a way to test just the connection portion of a php / mysqli connection. I am migrating from a LAMP server build on Vista to the same on Ubuntu and am havin

相关标签:
3条回答
  • 2020-12-10 16:22

    mysqli_connect() always returns a MySQLi object. To check for connection errors, use:

    $mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
    if ($mysqli_connection->connect_error) {
       echo "Not connected, error: " . $mysqli_connection->connect_error;
    }
    else {
       echo "Connected.";
    }
    
    0 讨论(0)
  • 2020-12-10 16:33

    For test php connection in you terminal execute:

    $ php -r 'var_dump(mysqli_connect("localhost:/tmp/mysql.sock", "MYSQL_USER", "MYSQL_PASS",
         "DBNAME));'
    
    0 讨论(0)
  • 2020-12-10 16:45

    You need more error handling on the various database calls, then. Quick/dirty method is to simply do

     $whatever = mysqli_somefunction(...) or die("MySQL error: ". mysqli_error());
    

    All of the functions return boolean FALSE if an error occured, or an appropriate mysqli object with the results. Without the error checking, you'd be doing:

     $result = $mysqli->query("blah blah will cause a syntax error");
     $data = $result->fetchRow();  // $result is "FALSE", not a mysqli_object, hence the "call to member on non-object"
    
    0 讨论(0)
提交回复
热议问题