turpentyne Posted September 21, 2010 Share Posted September 21, 2010 Ok, I'm a total newbie and I took a chance on writing a script off the top of my head to see how far I'd get. It wasn't very far. I have a search page that pulls results from a mysql database. next to each result is a link to see the complete info. This should go to a result page that pulls all the info from four different tables that match that ID and echoes it on the page. I should get one result and the fields I choose to show. But my while loop is reprinting the same item 20 or so times. Here's the quick script I tried. Don't cringe. I was trying this just off the top of my head: <html> <head> <title>login </title> </head> <body> <? include("header.php"); ?> <?php if (isset($_GET['id'])) { $plantid = $_GET['id']; require_once('databaseAccess.php'); $sql = "SELECT * FROM descriptors, plantae, relationships, habits WHERE plantae.plant_name LIKE '%$plantid%' AND descriptors.plant_id LIKE '%$plantid%' AND habits.plant_id LIKE '%$plantid%' AND habits.plant_id LIKE '%$plantid%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo '<table><tr> <td align="left">' . $row['scientific_name'] . '</td> <td align="left">' . $row['common_name'] . '</td> <td align="left">' . $row['leaf_shape'] . '</td> <td align="left">test' . $row['leaf_margin'] . '</td> <td align="left">' . $row['leaf_color'] . '</td> </tr>'; } echo '</table>'; } else { echo 'you have reached this page in error'; } ?> <? include("footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213977-while-loop-repeating-its-info/ Share on other sites More sharing options...
BoarderLine Posted September 21, 2010 Share Posted September 21, 2010 try changing to this [code=php:0] do { echo '<table><tr> <td align="left">' . $row['scientific_name'] . '</td> <td align="left">' . $row['common_name'] . '</td> <td align="left">' . $row['leaf_shape'] . '</td> <td align="left">test' . $row['leaf_margin'] . '</td> <td align="left">' . $row['leaf_color'] . '</td> </tr>'; } while($row = mysql_fetch_array($result)); [/code] Quote Link to comment https://forums.phpfreaks.com/topic/213977-while-loop-repeating-its-info/#findComment-1113581 Share on other sites More sharing options...
PFMaBiSmAd Posted September 21, 2010 Share Posted September 21, 2010 Your query is joining every row in descriptors with every row in plantae, then joining that with every row in relationships, and then joining that with every row in habits. Then you are only keeping the rows in all of that that match your WHERE condition. You need to specify which rows in each table are to be matched up. What columns define the relationships between each table? Edit: And since this is actually a query problem, moving thread to the mysql forum section. Quote Link to comment https://forums.phpfreaks.com/topic/213977-while-loop-repeating-its-info/#findComment-1113583 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.