Re: Missing first record in PHP/Mysql query

From: Date: Thu, 05 Jul 2001 22:40:10 +0000
Subject: Re: Missing first record in PHP/Mysql query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-56398@lists.php.net to get a copy of this message
At 7:11 PM -0700 7/5/01, Rory O'Connor wrote:
Excuse me if this is too newbie...but I'm writing a simple script to query a database and loop through the reuslts, echoing them on the page. When I enter the query at the mysql command line, I get the correct results. But the same query run through PHP renders all results except the first one. Any idea why?
It's doing exactly what you told it to ;) See below:
I've included the code snippet below: //the query to select the data $query = "SELECT firstname,email,optin from contact where interest='rory'"; $result = mysql_query($query); if(!$result) error_message(sql_error()); $query_data = mysql_fetch_row($result);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This line fetches the first row and moves the pointer to the next row. You then proceed to ignore the results of the first row! Just delete this line, and you'll be happy.
while($query_data = mysql_fetch_array($result)) {
        $firstname = $query_data["firstname"];
        $email = $query_data["email"];
        $optin = $query_data["optin"];
        echo "<BR>\n";
        echo "$firstname;$email;$optin\n";
} //end while loop also, what would be the code to output the reuslts to a text file instead of (or in addition to) the screen?
Check out the file functions - fopen, fwrite, fclose will be of particular interest: http://www.php.net/manual/en/ref.filesystem.php
thanks, rory providing the finest in midget technology
-- +------ Factoid: Of the 100 largest economies in the world, 51 are ------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +--- corporations ------ http://www.ips-dc.org/reports/top200text.htm ---+

« previous php.general (#56398) next »