Brunette
Brunette

Reputation: 27

php mysql query looping too many times on page load??

I am running a query to get the "forename" and "surname" of "manager" in the mysql below - the query works fine in mysql when I test it (it returns one result) however when the page loads on my browser the name appears more than 10,000 times! I am not sure what I am doing wrong - any help would be greatly appreciated, thankyou.

        <div id="">
        <?
            $query3 = "SELECT * FROM  `lf`.`weekly_report` INNER JOIN `lf`.`user_account` ON `lf`.`weekly_report`.`manager` = `lf`.`user_account`.`user_id` WHERE (DATE >= CURDATE( ) - INTERVAL DAYOFWEEK( CURDATE( ) ) +6 DAY) AND (DATE < CURDATE( ) - INTERVAL DAYOFWEEK( CURDATE( ) ) -1 DAY) AND  `p_code` = '" . $pCode . "'";
            echo "<table border=1 cellpadding=5 width=30% style='border-collapse:collapse; height:auto; font-size:small; float:left;'>";
            echo "<tr>";
            echo "<th style='background-color:#FFFFCC;'>Project Manager</th>";
            while ($row2 = mysql_fetch_assoc(mysql_query($query3))) {
                echo "<td style='background-color:#FFFFFF;text-align:left;'>" . $row2['forename'] . " " . $row2['surname'] . "</td>";
            }

            echo "</tr>";
            echo "</table>";
        ?>
        </div>

Upvotes: 1

Views: 223

Answers (1)

JRL
JRL

Reputation: 850

Try moving the query out of the loop.

$result3 = mysql_query($query3);
while ($row2 = mysql_fetch_assoc($result3)) {

You will also want to look into PDO, as the mysql_* functions are deprecated.

Upvotes: 4

Related Questions