Re: Pull from mysql_fetch_array at a given time

From: Date: Tue, 04 Dec 2001 01:41:31 +0000
Subject: Re: Pull from mysql_fetch_array at a given time
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-76447@lists.php.net to get a copy of this message
On Tue, 4 Dec 2001 11:30, Tom Beidler wrote: > I'm trying to pull three results from a database and then display them > in a page. The HTML is clugey so I would like to place print statements > in three different places, rather then in a while loop and try and > rewrite the HTML for each item. > > So far I have; > > $gethot_query = "SELECT title FROM events WHERE status = 'hot' ORDER BY > date"; > $gethot_result = mysql_query ($gethot_query) > or die ("Cannot get hot > items!"); > > $gethot_row = mysql_fetch_array ($gethot_result); > > ?> > <tr> > <td colspan="7" valign="top" bgcolor="#FFE49A" > class="hotheadlines"> <p><br> > <? print ("$gethot_row[1]"); ?></p> > > I know I'm not calling the print statement correctly but hopefully you > get the idea. I would like to add > <? print ("$gethot_row[1]"); ?> for the first item. > <? print ("$gethot_row[2]"); ?> for the second > <? print ("$gethot_row[1]"); ?> for the third. > > Am I approaching this from the wrong way? > > Thanks, > Tom First, I suspect that your query might fail, as you are attempting to sort by a filed you haven't selected. On to the meat of your question: I think you misunderstand how mysql_fetch_array works - it only fetches an array of the current record returned from the database, not an array with all the records found. What you might consider doing is retrieving all the records into an array, then displaying the elements of the array where you need them. Assuming that you are only interested in the filed 'title', something like this might do the job: $gethot_result = mysql_query ($gethot_query) or die ("Cannot get hot items! Error: " . mysql_error()); while ($gethot_row = mysql_fetch_array ($gethot_result)) { extract($gewthot_row); $your_array[] = $title; } and here you have all the values of title in $your_array. -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA This tagline will reformat your hard drive in 1.5 seconds!

« previous php.general (#76447) next »