问题
I'm stuck on something small, hope you can help me..
I need all phone numbers (thats a column in table 'leerlingen') in one here, with comma's separated, so I can insert that here in another script and it shows all phone numbers, like this:
0612345637,061231823,061231723
Code:
$result = mysql_query("SELECT * FROM leerlingen
WHERE klas_id='$aan_klas'");
while($row = mysql_fetch_array($result))
{
$variabele = $row['leerling_nummer'] . ",";
echo "$variabele";
}
But, when I echo $variabele
OUTSIDE the while loop, it only gives me ONE phone number.
When I echo $variabele
INSIDE the while loop, it shows me all the phone numbers, on the right way.. Any ideas?
Edit
my code isn't working at all I see, so here's what I want: It should get all phone numbers seperated with a comma, put in a variabele or array.
Edit
Thanks guys, it's workign! But... only when al mysql_ things ARE mysql_. When I change them to mysqli_, nothing happens...
回答1:
The reason why you are seeing all phone numbers when you echo "$variabele";
inside 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 $variabele
. And so, it looks as though everything was echo
'd all at once, when in fact it is echo
'd one at a time.
You can easily demonstrate this by altering the code to:
while($row = mysql_fetch_array($result))
{
$variabele = $row['leerling_nummer'] . ",";
echo "$variabele" . " | ";
}
Now, you will see the result is not (as I think you expected):
0612345637,061231823,061231723, |
... but:
0612345637, | 061231823, | 061231723, |
If what you believed was happening, was really happening, then the result would actually be this:
0612345637, | 0612345637, 061231823, | 0612345637, 061231823, 061231723, |
If you want to collect all phone numbers in one variable, you'd indeed be best off by using an array, as Mark Baker already proposed, or you would have to append each new phone number to the $variabele
string, like so:
// 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;
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.
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 GROUP_CONCAT()
. 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:
SELECT GROUP_CONCAT( leerling_nummer ) AS leerling_nummers
FROM leerlingen
WHERE klas_id='$aan_klas'
GROUP BY klas_id
After querying the database and then doing $row = mysql_fetch_array($result);
all numbers would then be in $row['leerling_nummers']
, seperated by a comma, because GROUP_CONCAT()
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.
回答2:
This is what arrays are designed for
$variabele = array();
while($row = mysql_fetch_array($result))
{
if($row['leerling_nummer'] != '')
$variabele[] = $row['leerling_nummer'];
}
echo implode(', ', $variabele);
回答3:
Use concatenation:
$result = mysql_query("SELECT * FROM leerlingen WHERE klas_id='$aan_klas'");
$variabele="";
while($row = mysql_fetch_array($result))
{
$variabele .= $row['leerling_nummer'] . ",";
}
echo "VAR: $variabele";
Aside from that, please consider using msqli_ or PDO since mysql_ functions are deprecated!
回答4:
According to the comments, it seems not working with mysqli
.
Here is a mysqli
solution:
<?php
$result = mysqli_query($link, "SELECT * FROM leerlingen WHERE klas_id='$aan_klas'");
$variabele = "";
while($row = mysqli_fetch_array($result))
{
if($row['leerling_nummer']){
$variabele .= $row['leerling_nummer'] . ",";
}
}
echo rtrim($variabele, ',');
?>
The mysqli_query()
expects the first parameter to be a link resource which returns from mysqli_connect()
.
<?php
$mysqlserver = "localhost";
$mysqlusername = "root";
$mysqlpassword = "";
$link = mysqli_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysqli_error());
?>
来源:https://stackoverflow.com/questions/14020132/get-all-data-from-mysql-row-in-a-variable