结果的var_dump给出空值.但是更深入的检查会返回整数 [英] var_dump of result gives null value. But deeper inspection returns a integer

查看:112
本文介绍了结果的var_dump给出空值.但是更深入的检查会返回整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
新的Mysqli对象为空

我刚开始为我的MVC框架构建数据库类.构建此文件时,我正在尝试使用简单的查询和表,以使其正常工作.

我正在尝试查询以下内容:

选择*来自mvc_test

这应该返回3行:

1 |测试

2 |测试2

3 |测试3

我使用以下方法查询:

<?php $this->result = $this->conn->query($this->q); ?>

$ this-> conn在哪里:

<?php 
 $this->conn = new mysqli($this->reg->conf->database['host'],
 $this->reg->conf->database['user'],
 $this->reg->conf->database['password'],
 $this->reg->conf->database['database']);
?>

其中$ this-> reg-> conf->数据库包含主机,数据库等的所有值.这有效,我有一个连接.

现在,当我var_dump这样的结果时:

<?php var_dump($this->result); ?>

我明白了:

object(mysqli_result)[9]

public'current_field'=>空

public'field_count'=>空

public'lengths'=> null

public'num_rows'=>空

public'type'=>空

但是,正如之前所说,它应该至少包含3行,所以我希望num_rows为'3'.

现在,当我var_dump这样的结果的num_rows时:

<?php var_dump($this->result->num_rows); ?>

我得到一个'int 3'作为答复.

结论:在第一个var_dump中它为null,但经过更深入的检查,我得到了3.因此它读取3行.当我添加另一行(第4行)时,它会按预期返回4.

我的问题是:为什么var_dump无法正常工作?为什么一开始会说null,但是经过更深入的检查,它确实有一个价值.

在此先感谢我,我真的很挣扎,因为我也没有遇到任何错误.

解决方案

该对象是懒惰地"实现的,即它直到真正需要时才从服务器检索数据(即,从结果对象访问某些内容). /p>

var_dump似乎没有触发属性获取器,因此它们显示为null.

Possible Duplicate:
New Mysqli Object is Null

I just started with building my database class for my MVC framework. While building this I am experimenting with simple queries and tables in order to make it work properly.

I was trying to query the following:

SELECT * FROM mvc_test

This should return 3 rows:

1 | test

2 | test2

3 | test3

I use the following method to query:

<?php $this->result = $this->conn->query($this->q); ?>

Where $this->conn is:

<?php 
 $this->conn = new mysqli($this->reg->conf->database['host'],
 $this->reg->conf->database['user'],
 $this->reg->conf->database['password'],
 $this->reg->conf->database['database']);
?>

Where $this->reg->conf->database contains all the values for host, database etc. This works, I have a connection.

Now, when I var_dump the result like this:

<?php var_dump($this->result); ?>

I get this:

object(mysqli_result)[9]

public 'current_field' => null

public 'field_count' => null

public 'lengths' => null

public 'num_rows' => null

public 'type' => null

But, as said before it should contain at least 3 rows, so I would expect num_rows to be '3'.

Now, when I var_dump the num_rows of the result like this:

<?php var_dump($this->result->num_rows); ?>

I get a 'int 3' as response.

Conclusion: in the first var_dump it's null, but with a deeper inspection I get a 3. So it reads 3 rows. When I add another row (the 4th), it returns a 4 as expected.

My question is: why is the var_dump not working properly? Why Does it say null at first, but with a deeper inspection it does have a value.

Thanks in advance, I'm really struggling with this one since I don't get any errors as well.

解决方案

The object is implemented "lazily", i.e. it does not retrieve data from the server until it's actually required (i.e. you access something from the result object).

var_dump doesn't seem to trigger the property getters so they are shown as null.

这篇关于结果的var_dump给出空值.但是更深入的检查会返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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