Php Mysqli->;取行问题
我从来没有在php中使用过新的mysqli类,因为旧的mysql接口总是足够好,但是我正在尝试更新一些旧代码来使用mysqli,事情进展得不太顺利,我得到了以下错误:Php Mysqli->;取行问题,php,mysql,mysqli,Php,Mysql,Mysqli,我从来没有在php中使用过新的mysqli类,因为旧的mysql接口总是足够好,但是我正在尝试更新一些旧代码来使用mysqli,事情进展得不太顺利,我得到了以下错误: [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_row() on a non-object in [location of file] 然而,我知道这个查询很好,因为我可以回显它,并直接在数据库中使用它,从而得到好的结果。
[error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_row() on a non-object in [location of file]
然而,我知道这个查询很好,因为我可以回显它,并直接在数据库中使用它,从而得到好的结果。我有一种感觉,我不正确地使用了“fetch_row()
有人能指出我做错了什么吗
/// class setup stuff
public function query($query,$sanitize=TRUE)
{
if($sanitize!==FALSE)
{
$query=$this->mysqli->escape_string($query);
}
echo $query;
$this->mysqli->query($query);
echo $this->mysqli->error;
//$this->mysqli->free();
}
public function select($table,$what,$where,$orderby=FALSE,$order=FALSE,$limits=FALSE,$sanitize=TRUE) //this is really simple and very limited
{
//process the select query and send to query method
$query = "SELECT $what FROM $table WHERE ";
$i = 0;
foreach($where as $key => $value)
{
$key = $sanitize?$this->mysqli->escape_string($key):$key;
$value = $sanitize?$this->mysqli->escape_string($value):$value;
$query .= "$key='$value' ";
$i++;
if($i<count($where))
{
$query .= "AND ";
}
}
// check the max rowcount
$this->query($query,FALSE);
$result = $this->mysqli->use_result(); //<--this is the issue
///类设置内容
公共函数查询($query,$sanitize=TRUE)
{
如果($sanitize!==FALSE)
{
$query=$this->mysqli->escape\u字符串($query);
}
echo$query;
$this->mysqli->query($query);
echo$this->mysqli->error;
//$this->mysqli->free();
}
公共函数select($table、$what、$where、$orderby=FALSE、$order=FALSE、$limits=FALSE、$sanitize=TRUE)//这非常简单,而且非常有限
{
//处理选择查询和发送到查询方法
$query=“从$table WHERE中选择$what”;
$i=0;
foreach($key=>$value)
{
$key=$sanitize?$this->mysqli->escape\u字符串($key):$key;
$value=$sanitize?$this->mysqli->escape\u字符串($value):$value;
$query.=“$key='$value'”;
$i++;
if($iquery($query,FALSE);
$result=$this->mysqli->use_result();//$this->mysqli未初始化多亏@scones的建议,我在初始化连接后测试了连接,发现问题在于“use_result”以及我没有在查询函数中返回结果。以下是工作版本通知$result=$this->query($query,FALSE);
公共函数查询($query,$sanitize=TRUE)
{
如果($sanitize!==FALSE)
{
$query=$this->mysqli->escape\u字符串($query);
}
echo$query;
如果($this->mysqli->error)
{
echo$this->mysqli->error;
出口
}
其他的
{
返回$this->mysqli->query($query);//mysqli->free();
}
公共函数select($table、$what、$where、$orderby=FALSE、$order=FALSE、$limits=FALSE、$sanitize=TRUE)//这非常简单,而且非常有限
{
//处理选择查询和发送到查询方法
$query=“从$table WHERE中选择$what”;
$i=0;
foreach($key=>$value)
{
$key=$sanitize?$this->mysqli->escape\u字符串($key):$key;
$value=$sanitize?$this->mysqli->escape\u字符串($value):$value;
$query.=“$key='$value'”;
$i++;
if($iquery($query,FALSE);//我在我的uu构造函数中使用它:$this->mysqli=new mysqli($this->host,$this->user,$this->pass,$this->name,$this->port);if(mysqli\u connect\u errno()){printf(“连接失败:%s\n,$this->mysqli->error”);退出()您可能希望在new之后的行中进行测试,如果成功的话,我可以在问题中转储整个类,但这对人们来说可能有点乏味?嗯,在再次查看您的代码之后:这两个函数不使用fetch_row()
。您的错误在其他地方,我同意scones的说法,错误在其他地方。可能是您可以使用PHP的debug_print_backtrace()在脚本到达fetch_row()之前查找调用的内容;
public function query($query,$sanitize=TRUE)
{
if($sanitize!==FALSE)
{
$query=$this->mysqli->escape_string($query);
}
echo $query;
if($this->mysqli->error)
{
echo $this->mysqli->error;
exit;
}
else
{
return $this->mysqli->query($query); //<-- needed this
}
//$this->mysqli->free();
}
public function select($table,$what,$where,$orderby=FALSE,$order=FALSE,$limits=FALSE,$sanitize=TRUE) //this is really simple and very limited
{
//process the select query and send to query method
$query = "SELECT $what FROM $table WHERE ";
$i = 0;
foreach($where as $key => $value)
{
$key = $sanitize?$this->mysqli->escape_string($key):$key;
$value = $sanitize?$this->mysqli->escape_string($value):$value;
$query .= "$key='$value' ";
$i++;
if($i<count($where))
{
$query .= "AND ";
}
}
$result = $this->query($query,FALSE); //<-- works now