I have this table:
option_values table:
option_id (FK)
value_id (PK)
classified_id (FK)
value
example:
option_id (FK) value_id (PK) classified_id (FK) value
1 1 22 'Petrol'
2 2 22 'Manual'
3 3 22 'Black'
How can I retrieve and echo every value found with the classified_id=22 ?
I have tried this:
$res=mysql_query("SELECT * FROM option_values WHERE classified_id=22");
$row = mysql_fetch_row($res);
echo $row[3]; // This displays 'Petrol'
But how can I also display the 'Manual', 'Black' etc?
Thanks
Use this, you need to get an array of rows...they way you are doing it is giving you a single row's value in an array.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Value: %s", $row[0], $row[3]);
}
Source: here
$res=mysql_query("SELECT * FROM option_values WHERE classified_id=22");
while(FALSE != ($row = mysql_fetch_row($res)) {
echo $row[3];
}
$res=mysql_query("SELECT * FROM option_values WHERE classified_id=22");
while($row = mysql_fetch_array($res))
{
echo $row[1];
echo $row[2];
echo $row[3];
}
Do yourself a favor and implement a helper function like so:
function fetch_multi_rows($query)
{
$rows = array();
while ( $row = mysql_fetch_array($result) )
$rows[] = $row;
return $rows;
}
// Use it like this
$my_rows = fetch_multi_rows(...your query...);
foreach ( $my_rows as $row )
echo $row[3];
Cheers!
You can write a "more object oriented" code like :)
// Execute the query $res = mysql_query("SELECT * FROM option_values WHERE classified_id=22"); // Check for empty result if (mysql_num_rows($result) == 0) { echo "No results."; exit; } // Fetch results as objects while ($row = mysql_fetch_object($result)) { echo "Option Id: ".$row->option_id; echo "Value: ".$row->value; } // Free results memory mysql_free_result($result);
Cheers Mate!
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.