簡體   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