pneudralics Posted June 2, 2009 Share Posted June 2, 2009 I remember browsing through some threads several weeks ago and someone mentioned that you can select one field without running it through a loop. Just curious what the syntax is for that. Thanks. Link to comment https://forums.phpfreaks.com/topic/160644-solved-how-do-i-select-just-one-field-from-the-database-without-looping/ Share on other sites More sharing options...
Kane250 Posted June 2, 2009 Share Posted June 2, 2009 Do you mean a select statement? SELECT field FROM tablename ?? Link to comment https://forums.phpfreaks.com/topic/160644-solved-how-do-i-select-just-one-field-from-the-database-without-looping/#findComment-847774 Share on other sites More sharing options...
pneudralics Posted June 2, 2009 Author Share Posted June 2, 2009 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']; } } Link to comment https://forums.phpfreaks.com/topic/160644-solved-how-do-i-select-just-one-field-from-the-database-without-looping/#findComment-847777 Share on other sites More sharing options...
Kane250 Posted June 2, 2009 Share Posted June 2, 2009 Well you can use foreach to get the data as well...that's what I use more often. I don't really know what you're doing with the data, but you're gonna have to loop through it somehow as far as I know. Link to comment https://forums.phpfreaks.com/topic/160644-solved-how-do-i-select-just-one-field-from-the-database-without-looping/#findComment-847780 Share on other sites More sharing options...
Jibberish Posted June 2, 2009 Share Posted June 2, 2009 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']; ?> Link to comment https://forums.phpfreaks.com/topic/160644-solved-how-do-i-select-just-one-field-from-the-database-without-looping/#findComment-847781 Share on other sites More sharing options...
lisa_85 Posted June 2, 2009 Share Posted June 2, 2009 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. Link to comment https://forums.phpfreaks.com/topic/160644-solved-how-do-i-select-just-one-field-from-the-database-without-looping/#findComment-847787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.