简体   繁体   English

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

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

My DB我的数据库

Name Title Name Title

Abu,Ali Red Shoes Abu,Ali Red Shoes

Mia,Sarah Yellow shoes Mia,Sarah Yellow shoes

I created a search bar using LIKE '%$keyword%'.我使用 LIKE '%$keyword%' 创建了一个搜索栏。 When enter keyword for example Abu or Red shoes the result i want to be appear is:当输入关键字例如阿布或红鞋时,我想要出现的结果是:

Abu

Red Shoes

Ali

Red Shoes

I only get the result like this:我只得到这样的结果:

Abu,Ali

Red Shoes


How can i separate the result without separating then in the DB...I need to store the data in comma value我怎样才能分离结果而不分离然后在数据库中......我需要将数据存储在逗号值中

after you get the values in php you can iterate over the names and use the explode function of php.在您获得 php 中的值后,您可以遍历名称并使用 php 的分解 function。 Please be aware that this database is set up to fail, it will very likely cause problems and headaches in the future.请注意,此数据库设置失败,将来很可能会导致问题和头痛。 It is better to seperate the names into individual rows.最好将名称分成单独的行。 However if you really cannot do it any other way, this is how you can seperate the names (I assume you get the SQL result into a variable called $result)但是,如果您真的不能以任何其他方式做到这一点,这就是您可以分隔名称的方式(我假设您将 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