解决mysql FIND_IN_SET 查询不到数据

PHP中文网
Release: 2023-02-28 11:26:01
Original
2434 people have browsed it


$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 class="brush:php;toolbar:false">";
print_r($res);
Copy after login

selectMsg那个函数可以输出,
但是下面城市的就不行,检查了一下不是表名之类的问题.字段也是utf8

这样查询也可以
解决mysql FIND_IN_SET 查询不到数据


解决了,在链接数据库后加入以下代码

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'");//写库
Copy after login

1 个回答

直接把SQL拿到数据库里面跑一下

类似这种

select * from city where FIND_IN_SET(city_name , '3213222222333,123'); 

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!