解决mysql FIND_IN_SET 查询不到数据
程序员文章站
2024-01-30 23:48:34
...
$con = mysql_connect("localhost","root","root"); //$dbname = "weixin"; function selectMsg($str) { $rows = array(); $msgArr = array(); $like = array(); $arr = array(); $sql = "SELECT * FROM weixin.weixin_msg WHERE FIND_IN_SET('".$str."',Title)"; $result = mysql_query($sql); while($rows = mysql_fetch_array($result, MYSQL_ASSOC)) { $msgArr[] = $rows; } if (!empty($msgArr)) { return $msgArr; }else{ $sqlstr = "SELECT * FROM weixin.weixin_msg WHERE Title LIKE '%".$str."%' order by id desc"; $like = mysql_query($sqlstr); while($arr = mysql_fetch_array($like, MYSQL_ASSOC)) { $msgArr[] = $arr; } return $msgArr; } } function cityName($keyword) { $sql = "SELECT * FROM weixin.weixin_cityname WHERE FIND_IN_SET('".$keyword."',CityNameCn)"; $result = mysql_fetch_array(mysql_query($sql), MYSQL_ASSOC); print_r($result); } cityName("北京"); $res= selectMsg("123"); echo "<pre>"; print_r($res);
selectMsg那个函数可以输出,
但是下面城市的就不行,检查了一下不是表名之类的问题.字段也是utf8
这样查询也可以
解决了,在链接数据库后加入以下代码
header("Content-Type: text/html;charset=utf-8"); //连接数据库 $con = mysql_connect("localhost","root","root"); mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
1 个回答
直接把SQL拿到数据库里面跑一下
类似这种
select * from city where FIND_IN_SET(city_name , '3213222222333,123');