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

Link to comment
Share on other sites

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

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.