新的Mysqli对象为空 [英] New Mysqli Object is Null

查看:92
本文介绍了新的Mysqli对象为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP中的Mysqli创建与MySQL数据库的连接.当我在独立页面上执行以下代码时:

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

我所有输出的是一个空的Mysqli对象,其中所有属性均为null.没有错误或显示任何内容.

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

我也没有在Apache或MySQL日志中看到任何条目.我对此有些迷茫.

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

推荐答案

我也遇到了这个问题,并且尝试对其进行调试非常疯狂.事实证明,有时无论出于什么原因,都不会填充mysqli对象,但是直接访问它的属性仍然会执行它后面的本机代码.因此,即使整个mysqli对象的var_dump显示空属性,但如果您分别访问它们,它们也会存在.如果errorno被证明是假的,则您可能执行了一个空的结果集,这是您所不期望的有效查询.希望这会有所帮助.

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

这篇关于新的Mysqli对象为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆