用逗号分隔记录

I'm new to php and I want to separate the records with a comma.

Database: the table in sql

I use this code to get the data:

<?php
$id = get_the_ID();
$sql = "SELECT * 
        FROM  tablename
        WHERE parent_id=$id";
$result = $conn->query($sql);

while($row = mysqli_fetch_array($result))
{
echo "Test: " . $row["value"]. "<br>";
}

mysqli_close($con);
?>

It return twice as:

Test: Test
Test: Test1

I want to separate the records with a ',' like this:

Test: Test, Test1

Store your values in an array and than implode with ",". You will get the result:

$id = get_the_ID();
$sql = "SELECT * 
        FROM  tablename
        WHERE parent_id=$id";
$result = $conn->query($sql);

$yourArr = array();
while($row = mysqli_fetch_array($result))
{
    $yourArr[] = $row["value"];
    //echo "Test: " . $row["value"]. "<br>";
}
echo "Test: ". implode(",",$yourArr);

you can use update query for this

$query="Update tablename set value=CONCAT(value,',test1') where parentid='295'";
$result=mysqli_query($conn,$query);

You can do this entirely in MySQL using GROUP_CONCAT:

<?php
$id = get_the_ID();
$sql = "SELECT `parent_id`, GROUP_CONCAT(`value` SEPARATOR ', ') AS comma_separated_values
        FROM tablename
        WHERE `parent_id` = '$id'
        GROUP BY `parent_id`";
$result = $conn->query($sql);
while ($row = mysqli_fetch_array($result))
{
    echo 'Test: ' . $row["comma_separated_values"]; // Test: test, test2
}
mysqli_close($con);
?>

You should change the "comma_separated_values" name to something more appropriate for your data.

In your particular case, you would not need the while() loop either as we're limiting the MySQL to a single row.

If you were to remove the WHEREparent_id= '$id' from the SQL query, then you could return the results of multiple parent_id values. For example:

while ($row = mysqli_fetch_array($result))
{
    echo 'Parent ' . $row['parent_id'] .': ' . $row["comma_separated_values"] . '<br>';
}

Would return:

Parent 292: Test1, Test2
Parent 293: Test3, Test4
Parent 294: Test5, Test10, Test50
etc...