繁体   English   中英

为什么不能通过foreach或get_object_vars访问PHP mysqli_stmt属性?

[英]Why can't PHP mysqli_stmt properties be accessed via foreach or get_object_vars?

我有以下代码:

//preparing statement, executing and so on is omitted...
//$this->cur_query_stmt is object returned by prepare() function
var_dump($this->cur_query_stmt);
foreach($this->cur_query_stmt as $k=>$v){
  var_dump($k);
  var_dump($v);
  }

此代码的输出:

object(mysqli_stmt)#6 (9) {
  ["affected_rows"]=>
  int(1)
  ["insert_id"]=>
  int(7)
  ["num_rows"]=>
  int(0)
  ["param_count"]=>
  int(1)
  ["field_count"]=>
  int(0)
  ["errno"]=>
  int(0)
  ["error"]=>
  string(0) ""
  ["sqlstate"]=>
  string(5) "00000"
  ["id"]=>
  int(2)
}
string(13) "affected_rows"
NULL
string(9) "insert_id"
NULL
string(8) "num_rows"
NULL
string(11) "param_count"
NULL
string(11) "field_count"
NULL
string(5) "errno"
NULL
string(5) "error"
NULL
string(8) "sqlstate"
NULL
string(2) "id"
NULL

谁能解释为什么所有值都返回为NULL?

我可以通过调用$ this-> cur_query_stmt-> $ this-> affected_rows来获取它们,但是当尝试将它们加入foreach或get_object_vars函数时,我得到了NULL。 我很困惑,请帮忙!

mysqli_stmt没有_constructor()来生成其属性值。 要获得“ affected_rows”,您必须改为调用$ this-> cur_query_stmt-> $ this-> affected_rows。

答案较晚,但我认为这是您想要做的:

   array_result=array();
   foreach ($stmt as $key => $value) {
       $array_result[$key]=$stmt->$key;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM