Chappers Posted June 15, 2007 Share Posted June 15, 2007 Hi, Embarrassingly, as it's been a few months since I tinkered with my code, I can't remember how to do this simple thing: When I've grabbed just one piece of data from a MySQL table, how do I display it? $query = ("SELECT age FROM members WHERE username='".$_SESSION['username']."'"); $result = mysql_query($query); echo ???????? Each username is unique so I know there is only one piece of data to be retrieved but can't for the life of me remember how to access it afterwards. Thanks for helping. Quote Link to comment https://forums.phpfreaks.com/topic/55659-solved-very-quick-easy-question-about-mysql-select-query/ Share on other sites More sharing options...
pocobueno1388 Posted June 15, 2007 Share Posted June 15, 2007 <?php $query = "SELECT age FROM members WHERE username='".$_SESSION['username']."'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo $row['age']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/55659-solved-very-quick-easy-question-about-mysql-select-query/#findComment-275032 Share on other sites More sharing options...
Chappers Posted June 15, 2007 Author Share Posted June 15, 2007 Thanks for that. For some reason I was sure I didn't need to do mysql_fetch_assoc if there was only one result being retrieved, I was thinking that's only for when more than one column was being retrieved from a row, such as in this example age and gender, for instance. Thanks again, back on track now and able to continue what I was doing. Quote Link to comment https://forums.phpfreaks.com/topic/55659-solved-very-quick-easy-question-about-mysql-select-query/#findComment-275035 Share on other sites More sharing options...
pocobueno1388 Posted June 15, 2007 Share Posted June 15, 2007 If there is more than one result, then you should use a while loop to display all the results as it will loop through each row. Don't forget to mark this thread as solved [bottom left corner] Quote Link to comment https://forums.phpfreaks.com/topic/55659-solved-very-quick-easy-question-about-mysql-select-query/#findComment-275044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.