Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

php - New Mysqli Object is Null

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:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$link = new mysqli('localhost', 'myuser', 'mypass', 'dbname');
var_dump($link);

All I get outputted is an empty Mysqli object where all the properties are null. No error or anything gets displayed.

I also don't see any entries in the Apache or MySQL logs. I'm kind of at a lost on this one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...