rfighter Posted June 28, 2008 Share Posted June 28, 2008 Hi, How can I select the top row of a MySQL query, without changing this select syntax: $newq = mysql_query("SELECT * FROM threadread WHERE userid = '$logged[id]' AND threadid = '$threadid' ORDER BY readtime DESC"); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/ Share on other sites More sharing options...
MasterACE14 Posted June 28, 2008 Share Posted June 28, 2008 what do you mean by 'top row' ? Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-576978 Share on other sites More sharing options...
rfighter Posted June 28, 2008 Author Share Posted June 28, 2008 The first row of the result of that select. Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-576979 Share on other sites More sharing options...
MasterACE14 Posted June 28, 2008 Share Posted June 28, 2008 I'm still not completely sure what you mean. But maybe your looking for 'LIMIT' <?php $newq = mysql_query("SELECT * FROM threadread WHERE userid = '$logged[id]' AND threadid = '$threadid' ORDER BY readtime DESC LIMIT 10"); Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-576982 Share on other sites More sharing options...
rfighter Posted June 29, 2008 Author Share Posted June 29, 2008 I'm still not completely sure what you mean. But maybe your looking for 'LIMIT' <?php $newq = mysql_query("SELECT * FROM threadread WHERE userid = '$logged[id]' AND threadid = '$threadid' ORDER BY readtime DESC LIMIT 10"); I would use that, and that is what I mean, but I also need to do a num_rows, to check if there are no results, and using a num_rows with a LIMIT returns a T_Variable error, so is there any other way please? Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-576992 Share on other sites More sharing options...
.josh Posted June 29, 2008 Share Posted June 29, 2008 post the code where you're trying to do num_rows because the query string is valid (except for the fact that you said you want just 1, not 10, so it would be "...desc limit 1") Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-577005 Share on other sites More sharing options...
rfighter Posted June 29, 2008 Author Share Posted June 29, 2008 post the code where you're trying to do num_rows because the query string is valid (except for the fact that you said you want just 1, not 10, so it would be "...desc limit 1") $newq = mysql_query("SELECT * FROM threadread WHERE userid = '$logged[id]' AND threadid = '$threadid' ORDER BY readtime DESC LIMIT 0,1"); $numrows = mysql_num_rows($newq); $newarray = $mysql_fetch_array($newq); The error now is: Fatal error: Call to undefined function: () in /home/xxx/public_html/newposts.php on line 44 Line 44 is "$newarray = $mysql_fetch_array...." EDIT: I realise what's wrong. I put a $ in front of mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-577008 Share on other sites More sharing options...
.josh Posted June 29, 2008 Share Posted June 29, 2008 well the only error I see there is you forgot the ; at the end of your fetch_array ... Quote Link to comment https://forums.phpfreaks.com/topic/112383-select-top-row-of-mysql-query/#findComment-577010 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.