• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 26
  • Last Modified:

my while loop is only returning 1 row when there is clearly 2 results

this is my query

$query_Recordset_links= "SELECT * FROM shipbook_links
LEFT JOIN tps_tour ON shipbook_links.shipbook_links_tps = tps_tour.tps_tour_id 
LEFT JOIN invoice_project ON shipbook_links.shipbook_links_invoice = invoice_project.invoice_project_id 
LEFT JOIN cc_rentals ON shipbook_links.shipbook_links_ccrental = cc_rentals.cc_rentals_id 
WHERE shipbook_links_order = $shipbook_id ORDER BY shipbook_links_active ASC ";
		 $result_Recordset_links= $link->query($query_Recordset_links);
		 $row_Recordset_links = $result_Recordset_links->fetch_assoc();

Open in new window

export it

 if(mysqli_num_rows($result_Recordset_links) >0) {
	  do {   	

echo $row_Recordset_links['tps_tour_id'];	  

	 } while ($row_Recordset_links = mysqli_fetch_assoc($Recordset_links)); 

Open in new window

now for testing, i use this

  echo mysqli_num_rows($result_Recordset_links);

Open in new window

and is echos 2..

but when i try to echo $row_Recordset_links['tps_tour_id'];
it only displays 1 result...
  • 2
1 Solution
Chris StanyonCommented:
In your while clause you are trying to fetch_array from $Recordset_links which doesn't appear to exist. I think you mean to use $result_Recordset_links.

Your code does look a little odd though - is there are reason why you're mixing object and procedural or why you're pulling out one record before doing the loop. Maybe this is a cleaner way to do it:

$results =  $link->query($query_Recordset_links);

while ( $row = $results->fetch_assoc() ) {
    echo $row['tps_tour_id'];	  

Open in new window

jblayneyAuthor Commented:
I'm trying to update older code without rewriting the page from scratch and it may be getting a bit messy ( it is all in mysql not mysqli)
Chris StanyonCommented:
OK. If you rewrite your code like I've posted then you'll get all the records from your query echoed out. There's no need to check the number of rows, as the while loop will only fire if you actually have rows.

Any trace of mysql should definitely be removed. If you don't do it, then your site is very likely to just die at some point. You can generally do a straight swap for the mysqli procedural style, but personally I find the object way of doing things feels cleaner and more logical.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now