I have the fallowing database table 'table_name':
+----+---------+--------------+
| ID | user_id | meta_key1 |
+----+---------+--------------+
| 1 | 1 | d |
| 2 | 1 | f |
| 3 | 1 | c |
| 4 | 2 | g |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
| 7 | 4 | 4 |
| 8 | 4 | 5 |
| 9 | 4 | 5 |
| 10 | 4 | 5 |
+----+---------+--------------+
I would like to select * from all entries where the user_id = '1'.
The code I'm using now returns only the first value:
$query = mysql_query("SELECT * FROM table_name WHERE user_id='1'");
$row = mysql_fetch_assoc($query);
while($row){
return $row["meta_key1"];
}
The result is always the first entry from the table_name where the user_id='(given_id)':
d
While I'd like this result:
d
f
c
You need to continually fetch a row in the loop to get every row in the result, similar to this:
while( $row = mysql_fetch_assoc($query)){
echo $row["meta_key1"];
}
Since you were doing return $row['meta_key1']
, as soon as the first row was fetched, the function returned with one value. So, if you're using this in a function, you'll need to build an array to get all of the values:
$return = array();
while( $row = mysql_fetch_assoc($query)){
$return[] = $row["meta_key1"];
}
return $return;
Change your code to :
while($row = mysql_fetch_assoc($query);){
echo $row["meta_key1"];
}
Currently, you only call mysql_fetch_assoc
one time. You need to call it every time you need the next row.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.