waynew Posted July 9, 2008 Share Posted July 9, 2008 Even when I'm only extracting one field from a table, I always use while($row = mysql_fetch_assoc($result)){ echo $row['example']; } Is there a better way to go about printing out only one value (where you know they'll be only one value) or do you have to always use that while loop? Link to comment https://forums.phpfreaks.com/topic/113936-printing-one-field/ Share on other sites More sharing options...
lemmin Posted July 9, 2008 Share Posted July 9, 2008 Just don't use the loop. $row = mysql_fetch_assoc($result); echo $row['example']; Link to comment https://forums.phpfreaks.com/topic/113936-printing-one-field/#findComment-585549 Share on other sites More sharing options...
MatthewJ Posted July 9, 2008 Share Posted July 9, 2008 If you mean one field... Then use your SELECT to only get the field you need. The loop is for displaying multiple records not multple fields. Link to comment https://forums.phpfreaks.com/topic/113936-printing-one-field/#findComment-585552 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 or $value = mysql_result($resource,$row_num,'col_name'); mysql_fetch_assoc and mysql_fetch_array return an [associative] array of the entire row. If you just want a single value, mysql_result gives it to you. just replace $row_num with 0 (zero) if only a single row is going to be returned. If the query only returns a single attribute, then you can leave the 'col_name' off as well. so something like <?php $query = "SELECT COUNT(`names`) FROM table"; $result = mysql_query($query); $num_names = mysql_result($result,0); ?> would set $num_names to the COUNT(`names`); Link to comment https://forums.phpfreaks.com/topic/113936-printing-one-field/#findComment-585564 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.