新的mysqli对象为空

问题描述:

我试图创建在PHP中使用的mysqli MySQL数据库的连接。当我一个独立的页面上执行以下代码:新的mysqli对象为空

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

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

我得到的输出是一个空的mysqli对象,其中的所有属性都为空。没有错误或任何显示。

我还没有看到在Apache或MySQL日志的所有条目。我对这个人很迷茫。

+0

试'$链接=新的mysqli( '本地主机', '为myuser', '为mypass', 'DBNAME')或死亡(mysqli_error());' – crush 2012-02-15 22:18:13

+1

@crush :我想你的意思是'$ link = new mysqli('localhost','myuser','mypass','dbname')或die(mysqli_connect_error());'。 'mysqli_error()'函数需要一个链接标识符作为其参数。 – 2012-02-15 22:24:37

+0

@Justin,我改变了你的建议,它根本没有改变。在实例化mysqli对象之后,假设没有错误,mysql_connect_error函数返回null。尽管mysqli对象的属性都是null。当我转储$ link时,得到: – 2012-02-15 22:37:45

我也有这个问题,快要疯了试图调试。事实证明,有时出于任何原因,mysqli对象没有被填充,但直接访问它的属性仍然执行它后面的本地代码。因此,即使整个mysqli对象的var_dump显示为空属性,如果您单独访问它们,它们仍然存在。如果errorno原来是错误的,那么您可能已经执行了一个有效的查询,其中包含一个空的结果集,而您并不期待。希望这可以帮助。

$mysqli = mysqli_connect('localhost', 'root', '', 'test', 3306); 

var_dump($mysqli); 

var_dump($mysqli->client_info); 
var_dump($mysqli->client_version); 
var_dump($mysqli->info); 

和输出:

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

刚刚遇到了这个问题,并且你的解决方案工作,但我确实发现'print_r'也会适当地打印出这个对象。 – user3158900 2014-06-06 13:52:56

我知道这是一个有点老,但任何人谁还有这是一个反复出现的问题,从user3158900到danperron的职位评论有解决方案。我在这里重新张贴,使其更加明显。此外,一些示例代码包括:

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>";