开发者

MySQL throws error after changing encoding [duplicate]

开发者 https://www.devze.com 2023-03-07 15:25 出处:网络
This question already has answers here: 开发者_开发百科 Closed 10 years ago. Possible Duplicate: mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
This question already has answers here: 开发者_开发百科 Closed 10 years ago.

Possible Duplicate:

mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I have the following php code

<?php
mysql_connect("localhost","admin","***");
mysql_select_db("DbName");
$sql=mysql_query("select * from `menu`");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

This code didn't work after I changed my database's encoding to UTF8-general_ci. What do I have to do in order to work my php code with my new database encoding?

It returns me the message:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in


My guess is that your database connection continues to be ISO-8859-1, while json_encode() expects UTF-8 data (and breaks if invalid characters are encountered).

Try sending the following query after connecting to the database:

If you're on mySQL < 5.0.7:

mysql_query("SET NAMES utf8;");

if you're on a newer mySQL:

mysql_set_charset("utf8");
0

精彩评论

暂无评论...
验证码 换一张
取 消