p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   Select statement looping results (http://p2p.wrox.com/showthread.php?t=82978)

Clint March 16th, 2011 05:10 AM

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.

om_prakash March 18th, 2011 09:43 AM

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 />";
}



All times are GMT -4. The time now is 08:56 AM.

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