Bibsta Bibsta - 4 months ago 10
MySQL Question

String from Mysql results

Im grabbing voltages from a database and displaying them on the page from the code below:

$separator = '';

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$volt = $separator.$row['volt'];
$separator = ',';
echo $volt;
}
}


This outputs the following:
12.34,12.45,13.01 etc how do I call this data outside the while loop?

If I echo $volt outside I get only one value ,12.34 for example not the full string?

Answer

Save your data with an array :

$volts = [];

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $volts[] = $row['volt'];
    }
}

echo implode(',',$volts);

I used implode() to join all elements of the array