在php中显示表中的数据

时间:2015-11-02 06:51:28

标签: php mysql sql mysqli

$sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
    $result = mysqli_query($con, $sql);
    if(mysqli_num_rows($result)>0)
        {
            echo "(";
            while($row = mysqli_fetch_assoc($result))
                {
                    echo  $row["location_tag"]; 
                    echo ",";
                }
            echo ")";   
        }

我上面的代码以一种格式显示数据,其中结果放在圆括号内,每个术语用逗号分隔

我得到的结果是这样的

(a,b,c,)

虽然看起来很好,但我希望逗号不应出现在c之后,因为它是这样的最后一个术语

(a,b,c)

任何人都可以告诉我们如何删除最后一个元素后的逗号

5 个答案:

答案 0 :(得分:2)

尝试使用PHP str_replace: -

$str='(a,b,c,)';
str_replace(",)",")",$str);

Demo

或尝试此代码: -

$var= "(";
while($row = mysqli_fetch_assoc($result))
{
$var.=  $row["location_tag"]; 
$var.= ",";
}
$var.= ")";  
echo str_replace(",)",")",$var);

答案 1 :(得分:1)

解决方案1:

您可以使用implode()函数和array()来完成。

背后的逻辑:

1)取一个空白数组。

2)以与现有结果相同的方式获取结果。

3)将结果附加到数组。

4)循环结束implode()数组后用逗号。

5)在结果中添加前置和待审核()

sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
$result = mysqli_query($con, $sql);
$arr = array();
if(mysqli_num_rows($result)>0) {
  while($row = mysqli_fetch_assoc($result)) {
    $arr[] = ["location_tag"]; 
  }
}
echo ! empty($arr) ? '(' . implode(',', $arr) . ')' : '';

解决方案2:

使用MYSQL的GROUP_CONCAT()(如果你只想从数据库表中获取一个字段)。

sql=" SELECT GROUP_CONCAT(location_tag) from `request_location` where requestid = '".$requestid."' ";
    $result = mysqli_query($con, $sql);
    $ids = '';
    if(mysqli_num_rows($result)>0) {
      while($row = mysqli_fetch_assoc($result)) {
        $ids = ["location_tag"]; 
      }
    }

echo ! empty($ids) ? '(' . $ids . ')' : '';

答案 2 :(得分:0)

使用IF条件并检查num_of_rows

$sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
if($count>0)
    {
        echo "(";
        $i = 1;
        while($row = mysqli_fetch_assoc($result))
            {
                echo  $row["location_tag"];
                if($i<$count) {
                 echo ",";
                }
              $i++;

            }
        echo ")";   
    }

答案 3 :(得分:0)

这个怎么样

$sql= "SELECT * from request_location where requestid = '$requestid'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
    {
        $new = ''
        while($row = mysqli_fetch_assoc($result))
        {
            $new .=  $row["location_tag"].",";
        }            

        $data =  "(".$new.")"; // Output - (a,b,c,)
        $new_data = str_replace(",)", ")", $data);
        echo $new_data ; // Output - (a,b,c)

    }

答案 4 :(得分:0)

要在两个字符串之间添加“,”,您可以使用implode(",", array_variable),如下所示:

$sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
    {
        while($row = mysqli_fetch_assoc($result))
            {
                $location_tag_arr[] = $row["location_tag"];
            }
        echo ")";   
    }

echo "(" . implode(",", $location_tag_arr) . ")";
//^output will be: (a,b,c)