删除最后一个逗号或防止它在所有MySQL / PHP上打印 [英] Remove last comma or prevent it from being printed at all MySQL/PHP

查看:231
本文介绍了删除最后一个逗号或防止它在所有MySQL / PHP上打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打印一组放在MySQL数据库中的单词,我用PHP检索它。我想把它作为一个逗号分隔的列表,但我需要它不打印或删除最后一个逗号。

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 remove the last comma. How could I do this?

我尝试使用 rtrim ,但我不大可能这样做。
这是我的代码,因为它是今天:

I did try to use rtrim, but I did not probably do it right. This is my code as it is today:

<?php

$query0  = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

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

echo "$keyword, ";
?>

我尝试使用 rtrim 尝试是这样的(我可能诚实地说,我在我的头上这个;))

I did try to use rtrim, my attempt was something like this (I might be honest enough to say that I am in above my head in this ;) )

$keyword = $row0['LCASE(ord)'];
$keywordc = "$keyword, ";
$keyword- = rtrim($keywordc, ', ');
echo "$keyword-, ";

正如你可能想象的,这没有打印太多(但至少它没有离开我空白页...)

As you might imagine, this did not print much (but at least it did not leave me with a blank page...)

推荐答案

我通常将结果放在数组中

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)'];
}

那么简单:

echo "My List: " . implode(', ', $some_array);
// Output looks something like:
My List: ord1, ord2, ord3, ord4

这篇关于删除最后一个逗号或防止它在所有MySQL / PHP上打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆