Jump to content


Photo

mysql_fetch_array() error


  • Please log in to reply
2 replies to this topic

#1 luddeb

luddeb
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 20 October 2005 - 09:14 PM

Hello,
I'm working to develop a new website but have run into some problems. I'm trying to build a links database, and until now everything has worked properly. I can add links to the database and then retrive them, but when I try to retrive a special number of them, they simply don't show.
Maybe some code could clear things up:
class links
{
  function show($number)
  {
    global $dbconn;
    $query = mysql_query("SELECT * FROM `links` WHERE `featured` = '1' ORDER BY visits DESC",$dbconn);
    $query2 = mysql_query("SELECT * FROM `links` WHERE `featured` = '0' ORDER BY visits DESC",$dbconn);
    echo '<table bgcolor="black" border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="2"><h3>Featured links</h3></td></tr>';
    while ($results = mysql_fetch_array($query))
    {
      echo '<tr><td><a href="index.php?dir=links&id=' . $results["id"] . '">' . $results["link"] . '</a></td><td rowspan="2"><p>Description: ' . $results["description"] . '</p></td></tr><tr><td colspan="2"><p>Visits: <b>' . $results["visits"] . '</b></p></td></tr>';
      if (login_check($_SESSION["user"],$_SESSION["pw"]) == "admin")
      {
        echo '<tr><td colspan="2"><a href="index.php?dir=acc&dir2=links&id=' . $results["id"] . '">Update/delete</a></td></tr>';
      }
      echo '<tr><td>&nbsp;</tr></td>';
    }
    echo '</table>';
    echo '<table bgcolor="black" border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="2"><h3>Non-featured links</h3></td>';
    if ($number == "all")
    {
      while ($results = mysql_fetch_array($query2))
      {
        echo '<tr><td><a href="index.php?dir=links&id=' . $results["id"] . '">' . $results["link"] . '</a></td><td rowspan="2"><p>Description: ' . $results["description"] . '</p></td></tr><tr><td><p>Visits: <b>' . $results["visits"] . '</b></p></td></tr>';
        if (login_check($_SESSION["user"],$_SESSION["pw"]) == "admin")
        {
          echo '<tr><td colspan="2"><a href="index.php?dir=acc&dir2=links&id=' . $results["id"] . '">Update/delete</a></td></tr>';
        }
        echo '<tr><td>&nbsp;</tr></td>';
      }
    }
    else
    {
      for ($i=0;$results = mysql_fetch_array($query2) && $i<$number;$i++) // <--- this query dosen't work :/
      {
        echo '<tr><td><a href="index.php?dir=links&id=' . $results["id"] . '">' . $results["link"] . '</a></td><td rowspan="2"><p>Description: ' . $results["description"] . '</p></td></tr><tr><td><p>Visits: <b>' . $results["visits"] . '</b></p></td></tr>';
        if (login_check($_SESSION["user"],$_SESSION["pw"]) == "admin")
        {
          echo '<tr><td colspan="2"><a href="index.php?dir=acc&dir2=links&id=' . $results["id"] . '">Update/delete</a></td></tr>';
        }
        echo '<tr><td>&nbsp;</tr></td>';
      }
    }
    echo '</table>';
  }
  // Other functions...
}

And as things couldn't get more strange (to me that is): Let's say i've got 3 links in my database and $number = "5" the function will loop three times, as it retrived the data but wasn't able to print it out :/.

thanks, luddeb

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 October 2005 - 10:19 PM

you have $results all over the place. maybe they get replaced while the loop runs. try to use different names.

i'd suggest you look into using LIMIT in the mysql query
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 luddeb

luddeb
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 21 October 2005 - 12:13 PM

thanks for your support, ryanlwh, everything works fine now. I skipped the for-loop and tried to limit the query instead by using the LIMIT statement, as you suggested. thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users