I have the following bit of code:
$query = "SELECT * FROM jos_vm_product_details WHERE global_variant_id = '$global_variant_id'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$row['name'];
}
Is it possible to select the $row['name'] only where something is matched:
Select $row['name'] WHERE id = $id
Any help would be appreciated
I don't fully understand your question, but you can limit that in the query to begin with:
$query = "SELECT * FROM jos_vm_product_details WHERE global_variant_id = '$global_variant_id' AND id = '$id'";
//-------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^
$result = mysql_query($query);
Otherwise if you need to limit it in the fetch loop, use something like:
while($row = mysql_fetch_array($result)) {
if ($row['id'] == $id) {
echo $row['name'];
}
}
But this is better handled in the query's WHERE
clause as in my first example.
You should change your SQL statement and append
AND id = '$id'
or, if you prefer, you can add a conditional statement within the while
loop to skip to the next row if the id doesn't match (not recommended)
if ($row['id'] != $id) continue
http://dev.mysql.com/doc/refman/5.5/en/select.html
$query = "SELECT name FROM jos_vm_product_details WHERE global_variant_id = '$global_variant_id'"
For the health of your application: never ever "filter" output in your application layer, your DB was designed to do all this stuff in the fastest and simplest way.
Do what Michael and Borodin said!