用php显示来自表的数据

时间:2021-06-16 03:46:44
$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 ")";   
        }

I have this above code that displays data in a format where the result is placed within round brackets and each term is separated by a comma

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

The result that i am getting is like this

我得到的结果是这样的

(a,b,c,)

Although it seems to be fine but i want that the comma should not appear after c as it is the last term like this

虽然看起来很好,但我想逗号不应该出现在c后面,因为它是最后一项

(a,b,c)

Can anyone please tell how to remove comma after the last element

有谁能告诉我在最后一个元素后如何去掉逗号吗

5 个解决方案

#1


1  

Solution 1:

解决方案1:

You can do it using implode() function and array().

可以使用内爆()函数和数组()来实现。

Logic behind it:

背后的逻辑:

1) Take a blank array.

1)取一个空数组。

2) Fetch the results the same way as existing.

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

3) Append results to the array.

3)将结果附加到数组中。

4) After the loop ends implode() the array with a comma.

4)循环结束后,内爆()以逗号结束数组。

5) Add a prepending and post pending ( and ) to the result.

5)在结果中添加预挂和post pending(和)。

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) . ')' : '';

Solution 2:

解决方案2:

Use MYSQL's GROUP_CONCAT() (if you want to fetch only one field from database table).

使用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


2  

Try this use PHP str_replace:-

尝试使用PHP str_replace:-

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

Demo

演示

or try this code :-

或者试试这个代码:-

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

#3


0  

Use an IF condition and check the num_of_rows

使用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 ")";   
    }

#4


0  

what about this

$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)

    }

#5


0  

For adding "," between two string you can use implode(",", array_variable), like below:

在两个字符串之间添加“,”可以使用内爆(“,”,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)

#1


1  

Solution 1:

解决方案1:

You can do it using implode() function and array().

可以使用内爆()函数和数组()来实现。

Logic behind it:

背后的逻辑:

1) Take a blank array.

1)取一个空数组。

2) Fetch the results the same way as existing.

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

3) Append results to the array.

3)将结果附加到数组中。

4) After the loop ends implode() the array with a comma.

4)循环结束后,内爆()以逗号结束数组。

5) Add a prepending and post pending ( and ) to the result.

5)在结果中添加预挂和post pending(和)。

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) . ')' : '';

Solution 2:

解决方案2:

Use MYSQL's GROUP_CONCAT() (if you want to fetch only one field from database table).

使用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


2  

Try this use PHP str_replace:-

尝试使用PHP str_replace:-

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

Demo

演示

or try this code :-

或者试试这个代码:-

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

#3


0  

Use an IF condition and check the num_of_rows

使用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 ")";   
    }

#4


0  

what about this

$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)

    }

#5


0  

For adding "," between two string you can use implode(",", array_variable), like below:

在两个字符串之间添加“,”可以使用内爆(“,”,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)