問題描述
我正在嘗試在 PHP 中使用 Mysqli 創(chuàng)建到 MySQL 數(shù)據(jù)庫的連接.當(dāng)我在獨(dú)立頁面上執(zhí)行以下代碼時(shí):
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);
我得到的只是一個(gè)空的 Mysqli 對(duì)象,其中所有的屬性都是空的.不會(huì)顯示任何錯(cuò)誤或任何內(nèi)容.
All I get outputted is an empty Mysqli object where all the properties are null. No error or anything gets displayed.
我也沒有在 Apache 或 MySQL 日志中看到任何條目.我有點(diǎn)不知所措.
I also don't see any entries in the Apache or MySQL logs. I'm kind of at a lost on this one.
推薦答案
我也遇到了這個(gè)問題,并且瘋狂地嘗試調(diào)試它.事實(shí)證明,有時(shí)出于某種原因,mysqli 對(duì)象沒有被填充,但直接訪問它的屬性仍然會(huì)執(zhí)行其背后的本機(jī)代碼.因此,即使整個(gè) mysqli 對(duì)象的 var_dump 顯示空屬性,如果您單獨(dú)訪問它們,它們也會(huì)存在.如果 errorno 結(jié)果為 false,則您可能執(zhí)行了一個(gè)有效查詢,但結(jié)果集為空,這是您不期望的.希望這會(huì)有所幫助.
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);
和輸出:
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
這篇關(guān)于新的 Mysqli 對(duì)象為空的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!