Jump to content

while loop repeating its info


turpentyne

Recommended Posts

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"); ?>


Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.