Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason why you are seeing <strong>all</strong> phone numbers when you <code>echo "$variabele";</code> <strong>inside</strong> the while loop, is because on each iteration (every time the while loop is re-entered, so to speak) you are echoing the last value of <code>$variabele</code>. And so, it <strong>looks</strong> as though everything was <code>echo</code>'d all at once, when in fact it is <code>echo</code>'d one at a time.</p> <p>You can easily demonstrate this by altering the code to:</p> <pre><code>while($row = mysql_fetch_array($result)) { $variabele = $row['leerling_nummer'] . ","; echo "$variabele" . " | "; } </code></pre> <p>Now, you will see the result is not (as I think you expected):</p> <p><code>0612345637,061231823,061231723, |</code></p> <p>... but:</p> <p><code>0612345637, | 061231823, | 061231723, |</code></p> <p>If what you believed was happening, was <strong>really</strong> happening, then the result would actually be this:</p> <p><code>0612345637, | 0612345637, 061231823, | 0612345637, 061231823, 061231723, |</code></p> <p>If you want to collect all phone numbers in one variable, you'd indeed be best off by using an array, as <a href="https://stackoverflow.com/questions/14020132/get-all-data-from-mysql-row-in-a-variabele#14020154">Mark Baker already proposed</a>, <strong>or</strong> you would have to append each new phone number to the <code>$variabele</code> string, like so:</p> <pre><code>// initiate $variabele as an empty string $variabele = ""; while($row = mysql_fetch_array($result)) { // append the new phone number to $variabele (note .= in stead of =) $variabele .= $row['leerling_nummer'] . ","; } echo $variabele; </code></pre> <p>The only problem with this, is that you'd have to remove the last comma then. There are a variety of ways to do this, or circumvent this, but Mark Baker's solution is much simpler actually.</p> <p>Lastly, I want to make you aware of a nifty MySQL function that allows you to easily concatenate values of a column; this is the function <code>GROUP_CONCAT()</code>. This, however, is probably not useful in your current case, since you are fetching other columns as well. But for future reference, if you only need one column want to concatenate them with a comma, you could do:</p> <pre><code>SELECT GROUP_CONCAT( leerling_nummer ) AS leerling_nummers FROM leerlingen WHERE klas_id='$aan_klas' GROUP BY klas_id </code></pre> <p>After querying the database and then doing <code>$row = mysql_fetch_array($result);</code> all numbers would then be in <code>$row['leerling_nummers']</code>, seperated by a comma, because <code>GROUP_CONCAT()</code> uses a comma as a default separator. In other words: you wouldn't need to loop over rows with PHP anymore; MySQL has already collected everything in one row for you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload