繁体   English   中英

在搜索栏中分别从 MySql 回显逗号分隔值

[英]Echo comma separated value from MySql in search bar separately

我的数据库

Name Title

Abu,Ali Red Shoes

Mia,Sarah Yellow shoes

我使用 LIKE '%$keyword%' 创建了一个搜索栏。 当输入关键字例如阿布或红鞋时,我想要出现的结果是:

Abu

Red Shoes

Ali

Red Shoes

我只得到这样的结果:

Abu,Ali

Red Shoes


我怎样才能分离结果而不分离然后在数据库中......我需要将数据存储在逗号值中

在您获得 php 中的值后,您可以遍历名称并使用 php 的分解 function。 请注意,此数据库设置失败,将来很可能会导致问题和头痛。 最好将名称分成单独的行。 但是,如果您真的不能以任何其他方式做到这一点,这就是您可以分隔名称的方式(我假设您将 SQL 结果放入名为 $result 的变量中)

if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        $names = explode(",", $row["Name"]);
        for($i = 0; $i < count($names); $i++){
            echo "Name: " . $names[$i] . "Title: " . $row["Title"];
            //In this for-loop you just iterate over the names and print them into 
            //the page. The first loop would be "Name: Abu Title: Red Shoes"
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM