Home > Backend Development > PHP Tutorial > Fatal error: Call to a member function fetch_array() on a non-object 错误

Fatal error: Call to a member function fetch_array() on a non-object 错误

WBOY
Release: 2016-06-20 12:49:34
Original
2043 people have browsed it

include_once ('conn.php');//error_reporting(0);@$shops_name= $_GET["shops_name"];if (isset($shops_name)) {	echo $shops_name;		$query = "SELECT * FROM goods WHERE ShopName= '$shops_name'";	@$q = $conn->query($query);		while($row=$q->fetch_array()) {				$select[]=array("goods_id"=>$row[ID],"goods_name"=>$row[Name]);			}	echo json_encode($select);}
Copy after login


将@query 复制到mysql查询成功。可是在这里失败了,求解。


回复讨论(解决方案)

贴出你的 conn.php

$conn = new mysqli("localhost","root","zxfang","test02");
if (!$conn) {
die("连接数据库失败:".mysqli_connect_error());
}

$conn->query("set character set 'gbk'");
$conn->query("set names 'gbk'");
?>

$q = $conn->query($query);
echo $q = $conn->error;

$q = $conn->query($query);
echo $q = $conn->error;



Illegal mix of collations (utf8_general_ci,IMPLICIT) and (gbk_chinese_ci,COERCIBLE) for operation '=

什么意思??

修改表字段编码后没有 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (gbk_chinese_ci,COERCIBLE) for operation '= 了,可还是Fatal error: Call to a member function fetch_array() on a non-object

@$q = $conn->query($query);
改为
$q = $conn->query($query) or die($conn->error);

看看有什么错误输出。

估计你的sql执行有问题。

@$q = $conn->query($query);
改为
$q = $conn->query($query) or die($conn->error);

看看有什么错误输出。

估计你的sql执行有问题。


原表有
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (gbk_chinese_ci,COERCIBLE) for operation '=' 错误。
表字段编码改成gbk后 $conn-error就没错误输出了,可还是有Fatal error: Call to a member function fetch_array() on a non-object 。

不要随意的修改有数据的表的字符集,因为修改字符集的动作并不会同时修改内容的字符集
请改回盐来得字符集设置,以免数据丢失!

$query = "SELECT * FROM goods WHERE ShopName= '$shops_name'";

echo base64_encode($query);
贴出结果(当然是要是出错时候的)

不要随意的修改有数据的表的字符集,因为修改字符集的动作并不会同时修改内容的字符集
请改回盐来得字符集设置,以免数据丢失!

$query = "SELECT * FROM goods WHERE ShopName= '$shops_name'";

echo base64_encode($query);
贴出结果(当然是要是出错时候的)


U0VMRUNUICogRlJPTSBnb29kcyBXSEVSRSBTaG9wTmFtZT0gJ+W6lzEn

echo base64_decode('U0VMRUNUICogRlJPTSBnb29kcyBXSEVSRSBTaG9wTmFtZT0gJ+W6lzEn');
Copy after login

gbk 环境中: SELECT * FROM goods WHERE ShopName= '搴?1'
utf-8 环境中: SELECT * FROM goods WHERE ShopName= '店1'
显然你传入的就是 utf-8 编码的数据
你还当做 gbk 编码处理($conn->query("set names 'gbk'");)能不出错吗?

哦,原来是这样,我对于编码一直都不是很了解。
好了,问题解决了,谢谢版主。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template