MySQL throws error after changing encoding [duplicate]
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");
精彩评论