Jump to content

Recommended Posts

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!  :D

 

Mark.

 

Link to comment
https://forums.phpfreaks.com/topic/243931-displaying-results-from-mysql-database/
Share on other sites

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.

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.

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.