PHP mysqli_error传递boolean作为参数1 [复制]

I'm a PHP beginner so please be gentle :-)

I'm experimenting with mysqli_error. The following code snippet checks connections $cxn.

  7 ini_set('display_errors',1);
  8 error_reporting(E_ALL);
  9
 10
 11 echo "<html>
 12       <head><title>Test MySQL</title></head>
 13       <body>";
 14 $host = "localhost";
 15 $user = "test";
 16 $password = "test";
 17 $database = "PetCatalogFail";
 18
 19 $cxn = mysqli_connect($host,$user,$password,$database);
 20
 21 if(!$cxn)
 22 {
 23         var_dump($cxn);
 24
 25         $message = mysqli_error($cxn);
 26         echo "$message";
 27         echo "</body>";
 28         echo "</html>";
 29         die();
 30 }

$database is deliberately chosen to be incorrect in an attempt to make $cxn False. However I am getting the following message:

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /var/www/test.com/public_html/temp.php on line 25

var_dump($cxn) gives output as bool(false). I was hoping to store a resulting error message in $message and echo it. Am I using mysqli_error incorrectly?

</div>

mysqli_error(); returns error from MySQL operation.

Use mysqli_connect_error(); for connection.

Example : http://php.net/manual/en/mysqli.connect-error.php

According to the documentation mysqli_error() expects a mysqli instance to be passed as first argument. To me it looks like your mysqli_connect returns (bool) false and you pass it to the mysqli_error() function.

To avoid error like this you may use the "object oriented" style of the mysqli library:

<?php
$connection = new mysqli($host, $user, $password, $database);

// to read a connection error access the member properties
$connection->connect_errno;    

// to read the error
print $connection->error;