mysqli_free_result():mysqli_result类的对象无法转换为字符串
问题描述:
就我对Google和所有内容所做的搜索而言,它看起来像是一个很常见的问题,但似乎无法修复它。另外,我认为我和其他人有不同的用法。 而且,经过大约3个小时的运气,我在这里张贴!mysqli_free_result():mysqli_result类的对象无法转换为字符串
function free_result(){ # LINE 48
$free = "SELECT SHOW DESCRIBE EXPLAIN"; # LINE 49
$free = explode(" ", $free); # LINE 50
$sql = $this->sql; # LINE 51
while(list($key, $value) = each($free)){ # LINE 52
if(preg_match("/\b".$value."\b/", $sql)){ # LINE 53
$result = $this->result; # LINE 54
if(!mysqli_free_result($result)){ # LINE 55
$this->errors("Invalid result: <b>{$result}</b>. Couldn't free result."); # LINE 56
} # LINE 57
} # LINE 58
} # LINE 59
} # LINE 60
# LINE 61
function query($sql){ # LINE 62
$this->query_id = mysqli_query($this->connection, $sql); # LINE 63
$this->result = mysqli_store_result($this->connection); # LINE 64
$this->sql = $sql; # LINE 65
if(!$this->query_id){ # LINE 66
$this->errors("Couldn't query: <b>{$sql}</b>"); # LINE 67
return 0; # LINE 68
} # LINE 69
$this->affected = mysqli_affected_rows($this->connection); # LINE 70
# LINE 71
return $this->query_id; # LINE 72
} # LINE 73
这些是我的数据库类中的2个函数。但我认为只有这2个才能解决问题。
所以,我recieving错误是:
"Warning: mysqli_free_result() expects parameter 1 to be mysqli_result,
boolean given in [file path]\database.class.php on line 55"
#followed by my database class error handling
"Invalid result: . Couldn't free result."
至于我对这个去理解,我觉得现在的问题是$结果变量(54行,64行),但因为这是我对MySQLi的第一次冒险,所以我不太确定。
我希望你能理解这个问题,并且能够提供帮助! 在此先感谢!
答
mysqli_store_result
返回2例一个布尔值:用于更新/插入/删除等查询它返回true(和这些查询可以包含一个SELECT/SHOW/DESCRIBE /解释,例如一个字符串),或SELECT/SHOW/DESCRIBE/EXPLAIN查询失败。检查$this->sql
会告诉你哪个。如果你已经开始解放这些结果了,只需在之前做一个更简单的检查:
function free_result(){
if($this->result instanceof mysqli_result) $this->result->free();
}
答
该代码看起来是正确的,但store_result可以在没有结果集(例如插入)或错误时返回FALSE。如果mysqli_field_count
非零,则应该已返回结果集。它看起来像使用$ free数组检查这个(但field_count应该更快,更准确)。如果mysqli_error
返回一个非空字符串,则出现错误。
谢谢!完美的作品...再次证明简单更好。呵呵。 – jolt 2010-07-16 02:21:20