New Mysqli Object is Null

前端 未结 2 1401
醉酒成梦
醉酒成梦 2020-12-16 04:16

I\'m trying to create a connection to a MySQL database using Mysqli in PHP. When I execute the following code on a stand alone page:



        
相关标签:
2条回答
  • 2020-12-16 04:32

    I know this is a bit old, but for anyone who still has this as a recurring issue, the comment from user3158900 to danperron's post has the solution. I'm re-posting here to make it more visible. Also, some example code is included:

    global $mysqli; //assuming you've already created your $mysqli object
    $mysqli->query("INSERT INTO users SET username='jim' and password='somehash'");
    var_dump( $mysqli ); //prints the full object but all the values are null 
    
    //the following code prints the full object with appropriate values such as
    //insert_id, affected_rows, etc
    //view http://pastebin.com/Mgd2CZsM for an example of this output
    echo "<pre>" . print_r( $mysqli, true ). "</pre>"; 
    
    0 讨论(0)
  • 2020-12-16 04:41

    I had this problem too and was going crazy trying to debug it. It turns out that sometimes for whatever reason the mysqli object does not get populated, but directly accessing it's properties still executes the native code behind it. So even though a var_dump of the entire mysqli object shows null properties, they are there if you access them individually. If errorno turns out to be false, you may have executed a valid query with an empty resultset that you weren't expecting. Hope this helps.

    $mysqli = mysqli_connect('localhost', 'root', '', 'test', 3306);
    
    var_dump($mysqli);
    
    var_dump($mysqli->client_info);
    var_dump($mysqli->client_version);
    var_dump($mysqli->info);
    

    and the output:

    object(mysqli)[1]
      public 'affected_rows' => null
      public 'client_info' => null
      public 'client_version' => null
      public 'connect_errno' => null
      public 'connect_error' => null
      public 'errno' => null
      public 'error' => null
      public 'field_count' => null
      public 'host_info' => null
      public 'info' => null
    
    
    public 'insert_id' => null
      public 'server_info' => null
      public 'server_version' => null
      public 'stat' => null
      public 'sqlstate' => null
      public 'protocol_version' => null
      public 'thread_id' => null
      public 'warning_count' => null
    
    string 'mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $' (length=50)
    int 50008
    null
    int 0
    string 'localhost via TCP/IP' (length=20)
    string '5.5.20-log' (length=10)
    int 50520
    
    0 讨论(0)
提交回复
热议问题