Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 16th, 2011, 05:10 AM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default Select statement looping results

Good morning! :)

I have a select statement that is returning the correct results but is looping the retrieved data over and over....

This is what I have:

Code:
<?php
$id = $_GET['id'];
$result = mysql_query("SELECT jobs.job_title,jobs.date,jobs.id,users.full_name,users.id FROM `jobs`,users WHERE jobs.user_id = $id ORDER BY job_title DESC LIMIT 10") or die(mysql_error()); 
echo "<table border='1'>";
echo "<tr> <th>Job Title</th> <th>Date Added</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['job_title'];
	echo "</td><td>"; 
	echo $row['date'];
	echo "</td></tr>"; 

} 

echo "</table>";
?>
The result that it is returning looks something like this:


Job Title Date Added
Web Developer 2011-02-17
Web Developer 2011-02-17
Web Developer 2011-02-17
Sales Rep 2011-02-15
Sales Rep 2011-02-15
Sales Rep 2011-02-15
PHP Programmer 2011-03-07
PHP Programmer 2011-03-07
PHP Programmer 2011-03-07
Accountant 2011-02-15

As you can see it displays each record 3 times and then the next and so forth.

Any ideas how to change this would be highly appreciated.

Thank you in advance.
  #2 (permalink)  
Old March 18th, 2011, 09:43 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

the code seems to be ok, please run the SQL query separately and see it returns the results correctly.

Code:
while($row = mysql_fetch_array($result)){
	echo $row['job_title']. " - ". $row['date'];
	echo "<br />";
}
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping Statement firefighter2045 Access VBA 13 January 18th, 2008 04:27 PM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Looping Statement Solution KKeesling Visual Basic 2005 Basics 0 April 2nd, 2006 12:50 AM
Getting results from Select Multiple HTML tag dpjoff PHP How-To 9 February 2nd, 2004 04:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.