How would I get all the values of a field in mysql using php?
I have 3 rows with 3 fields, 开发者_如何学运维one row is numbers, how do I get all of those numbers? in that field? LIKE mysql_fetch_rows() in a for loop doesn't work?
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
In this code, it was selected a "person" from table "people" that had id = 42
. Only one row of results is expected so, all that is needed is $row = mysql_fetch_row($result);
.
After this $row
will be something like this: array(0 => 42, 1 => 'person@people.xx');
.
To print the email, you access the second position of $row
by echoing $row[1]
.
This is exactly what's going on php documentation about mysql_fetch_row()
On the other hand, if you expect several rows to be returned, from a query like: SELECT id, email FROM people WHERE email LIKE '%@people%'
, you should use mysql_fetch_array()
with a while
loop. You can see example at mysql_fetch_array()
documentation
If you are asking how to get all three numerical values from three different rows in one result you should look into MySQL's GROUP_CONCAT()
精彩评论