Jump to content

Query always loops the first row


PHPiSean

Recommended Posts

Here's the code, I had used the query in the header, and now I'm using it to display the links in a table. What happens is that it only returns the first row and not the second third or fourth rows. When it does display the first row, it'll display it a lot of times. Here it is

 

<?php
    if($_GET['place']==addnetwork){
        //display current networks 
        echo "<table border=1>";
        echo "<tr><td>Network Name</td><td>URL</td></tr>";
        
        while ($query1 = mysql_fetch_assoc(mysql_query("select name,url from s_links"))) {
            $networkname = $query1['name'];
            $networkurl = $query1['url'];

            
            echo "<tr><td>$networkname</td><td>$networkurl</td></tr>";
            
        }
        echo "</table>";
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/246136-query-always-loops-the-first-row/
Share on other sites

<?php
    if($_GET['place']==addnetwork){
        //display current networks 
        echo "<table border=1>";
        echo "<tr><td>Network Name</td><td>URL</td></tr>";
        
        $result = mysql_query("select name,url from s_links");
        while ($query1 = mysql_fetch_assoc($result)) {
            $networkname = $query1['name'];
            $networkurl = $query1['url'];

            
            echo "<tr><td>$networkname</td><td>$networkurl</td></tr>";
            
        }
        echo "</table>";
    }
?>

That loop will continue indefinitely! You need to run the query ONE TIME and then do a while loop over the RESULTS. You are currently running a new query on each iteration of the loop and only getting the first result.

 

    if($_GET['place']==addnetwork)
    {
        //Create/execute query
        $query = "SELECT name, url FROM s_links";
        $result = mysql_query($query) or die("Error running query: " . mysql_error());

        //display current networks 
        echo "<table border=1>\n";
        echo "<tr><th>Network Name</th><th>URL</th></tr>\n";
        //Loop through results
        while ($row = mysql_fetch_assoc$result())
        {
            echo "<tr><td>{$row['name']}</td><td>{$row['url']}</td></tr>\n";
        }
        echo "</table>\n";
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.