我有以下代码:
//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。我很困惑,请帮忙!
答案 0 :(得分:0)
mysqli_stmt没有_constructor()来生成其属性值。要获得“affected_rows”,您必须拨打$ this-> cur_query_stmt-> $ this-> affected_rows。
答案 1 :(得分:0)
最新答案,但我认为这是您想要做的:
array_result=array();
foreach ($stmt as $key => $value) {
$array_result[$key]=$stmt->$key;
}