Remove last comma or prevent it from being printed at all MySQL/PHP

前端 未结 3 382
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 10:36

I am printing a set of words that is placed in a MySQL database and I am retrieving it with PHP. I want to present it as a comma separated list, but I need it not to print or re

相关标签:
3条回答
  • 2021-01-24 10:55

    I usually do this by placing the results in an array first

    $some_array = array();
    while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC)) {
       $some_array[] = $row0['LCASE(ord)'];
    }
    

    then simply:

    echo "My List: " . implode(', ', $some_array);
    // Output looks something like:
    My List: ord1, ord2, ord3, ord4
    
    0 讨论(0)
  • 2021-01-24 11:00

    I would do:

    $keywords = array();
    while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
    {
       $keywords[] = $row0['LCASE(ord)'];
    }
    
    echo implode(',', $keywords);
    
    0 讨论(0)
  • 2021-01-24 11:14
    substr($string, 0, -1);
    

    That removes the last character.

    0 讨论(0)
提交回复
热议问题