Re: displaying a select number of rows from a sql query

From: Date: Thu, 10 Oct 2002 02:09:19 +0000
Subject: Re: displaying a select number of rows from a sql query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-119486@lists.php.net to get a copy of this message
With the line: while (list($link, $title, $content) = mysql_fetch_array($result)) { The php docs don't actually say what the list() function returns, but since your getting an infinite loop i would say that it probably evaluates to true no matter whats on the right hand side of the assignment. I would suggest that you use: while ($tmp = mysql_fetch_array($result)) { list($link, $title, $content) = $tmp; Cheers, Owen Prime http://www.noggin.com.au Keith Posehn wrote: > Ok, here is the question: > > I have a sql query, nothing special. It has 3 variables. I have created > the php code to display the variables in context with the html code. I > need it to loop a select number of times, most likely twice, and therefore > display the first two rows of the query. > > In other words, querying the database for a set of rows, it needs to > display the first two of the rows on the page. > > Here is the code I have tried so far, which has either looped continuosly > or displayed the top row twice (edited for security): > > <?php > > > $sql = "SELECT * FROM [database] ORDER BY [collumn] LIMIT 2"; > $result = mysql_query($sql) or die("problem with $sql"); > // list($title) = mysql_fetch_array($result); > ?> > > <td width=48% align=left valign=top> > > <h3>What's New?</h3> > > <?php > > /* It tried this: */ while(list($link, $title, $content) = > mysql_fetch_array($result)){ > > /* And this: */ while($row = mysql_fetch_array($result) and > (($count++)<2)){ > > // The first kept looping infinitely, the second displayed the top row > twice. Grr... >_< > > ?> > > <h4> > <a href="<?php echo mysql_result($result,0,"link"); > ?>"><?php echo > mysql_result($result,0,"title"); ?></a> > </h4> > <p> > <?php echo mysql_result($result,0,"content"); ?> > </p> > > <?php > } > ?> > > </td> > > Thanks for any help you can provide. > > -Keith Posehn

« previous php.general (#119486) next »