Jump to content

[SOLVED] How do I select just one field from the database without looping?


pneudralics

Recommended Posts

If all I need is the field 'id' ... I thought there was a more simple way of selecting only one field so I don't have to do like the following...

Instead of doing:

$q = "SELECT id FROM table";
if ($r = mysql_query($q)) {

while ($row = mysql_fetch_array ($r)) {
$id= $row['id'];
} 

}

 

 

Well it depends on if you have a set of results or just one result. if you have a set of results your going to have to loop through them. Else if you know only one is being returned I think you can just do.

 

<?php
$row = mysql_fetch_assoc($r);
$id = $row['id'];
?>

 

 

If you know the specific id of the field and you only want to return one row then you don't need to use a loop

$fID = 1;
$result = mysql_query("SELECT fID FROM tblfiles WHERE fID=$fID");
$field = mysql_fetch_array($result);
echo 'field = '.$field['fID'];

 

If you want to return more than one row, you need a loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.