nortksi Posted August 5, 2011 Share Posted August 5, 2011 Hi guys, I'm hoping someone can help me with little problem I'm having 'echo'ing results retrieved from a mySQL database. I know there are 4 results being returned but my code only displays 3 of them. Any ideas, it's probably really simple but I am just leaning. My code is below: <?php $rt=mysql_query($query); echo mysql_error(); $nt=mysql_fetch_array($rt); $results=@mysql_num_rows($rt); if ($results > 0) { for ($ii = 0; $ii < $results; $ii++){ $data = mysql_fetch_array($rt); $dr [] = $data; } } if ($results > 1) { for ($ii = 0; $ii < $results; $ii++){ echo $dr[$ii]['event_name'];?><br /><?php } }else {} ?> Thanks in advance! Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/ Share on other sites More sharing options...
nortksi Posted August 5, 2011 Author Share Posted August 5, 2011 I forgot to mention that it is the first result not being displayed. Regards, Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252525 Share on other sites More sharing options...
WebStyles Posted August 5, 2011 Share Posted August 5, 2011 You seem to be confusing a lot of things. keep it simple. <?php $rt=mysql_query($query); while($data = mysql_fetch_assoc($rt)){ $dr[] = $data; echo $data['event_name'] .'<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252527 Share on other sites More sharing options...
nortksi Posted August 5, 2011 Author Share Posted August 5, 2011 Hi WebStyles, thanks for the reply, and tip. However; it still only displays 3 out of the 4 results, again it is the first result in the mySQL database that isn't being displayed. Regards, Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252531 Share on other sites More sharing options...
@ Posted August 5, 2011 Share Posted August 5, 2011 any chance the event name for the first row is empty? otherwise id say the issues is in your query. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252534 Share on other sites More sharing options...
WebStyles Posted August 5, 2011 Share Posted August 5, 2011 instead of this line: echo $data['event_name'] .'<br />'; try this: echo trim($data['event_name']) != '' ? $data['event_name'] .'<br />' : 'Database contains empty event name here<br />' ; Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252535 Share on other sites More sharing options...
nortksi Posted August 5, 2011 Author Share Posted August 5, 2011 any chance the event name for the first row is empty? otherwise id say the issues is in your query. Hi. The first row is not empty. This has really confused me as I know 4 results have been retrieved from the database because I tested it by echoing $results which displayed 4. I have to be careful what I write on here as I'm signed into a non-disclosure agreement so I don't know how much of the code I can put on here. Kind regards, Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252537 Share on other sites More sharing options...
nortksi Posted August 5, 2011 Author Share Posted August 5, 2011 instead of this line: echo $data['event_name'] .'<br />'; try this: echo trim($data['event_name']) != '' ? $data['event_name'] .'<br />' : 'Database contains empty event name here<br />' ; Hi, OK I tried this, exactly the same result Thx Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252540 Share on other sites More sharing options...
WebStyles Posted August 5, 2011 Share Posted August 5, 2011 what's in $query ? are you doing a JOIN of some sorts ? (please post the rest of the code) Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252544 Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2011 Share Posted August 5, 2011 Your original code contained an extra $nt=mysql_fetch_array($rt); statement that was fetching and discarding the first row from the result set. Even though WebStyles posted a much simplified and commonly used way of executing a query and looping over the result set, I doubt that you changed all your code to use just those lines of code. Post your current code, from the line where you are executing the query through to the end of the loop you are using to iterate over the result set. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252548 Share on other sites More sharing options...
nortksi Posted August 5, 2011 Author Share Posted August 5, 2011 Thanks for you help guys, I finished work early today and wont be back in until Monday morning. I'll get onto your suggestions first thing monday morning and I'll post the results. Thx again guys. Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1252841 Share on other sites More sharing options...
nortksi Posted August 6, 2011 Author Share Posted August 6, 2011 Your original code contained an extra $nt=mysql_fetch_array($rt); statement that was fetching and discarding the first row from the result set. OK I couldn't resist, I took my work home and tried removing one of the $nt=mysql_fetch_array($rt); and this did the trick! Thanks PFMaBiSmAd and everyone else for their help, much appreciated If you get time, PFMaBiSmAd, will you be able to explain why having 2 arrays would dicard the first row? Kind regards, Mark. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1253168 Share on other sites More sharing options...
PFMaBiSmAd Posted August 6, 2011 Share Posted August 6, 2011 There's nothing to explain. You are calling a mysql_fetch_ statement before the start of your loop, so of course the first row in the result set is missing. It has already been fetched. Computers only do exactly what their code tells them to do. Quote Link to comment https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/#findComment-1253293 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.