Re: while() looping over query results twice?

From: Date: Thu, 25 Oct 2001 19:50:31 +0000
Subject: Re: while() looping over query results twice?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-72339@lists.php.net to get a copy of this message
Trying using mysql_fetch_array, and using the field names. This is the recommended behaviour in PHP4:
       $result = mysql_query($myQuery) //$myQuery gets defined earlier
       $resultCount = mysql_num_rows($result);
       $companyID = array();
           while($query_data = mysql_fetch_array($result)) {
               $companyID[$query_data['CompanyID']] = $query_data['CompanyName'];
               $companyID_string .= "," . $query_data['CompanyID'];
               $companyName_string .= "," . $query_data['CompanyName'];
           }
That probably isn't the problem, but hey.. you never know. I also noticed there's no ; after mysql_query ;) Lastly, you could also do (for debugging): while ($query_data = mysql_fetch_array($result)) { $tmp[] = $query_data; } echo '<pre>'; ob_start(); print_r($tmp); $text = ob_get_contents(); ob_end_clean(); echo htmlspecialchars($text); echo '</pre>'; To see what exactly is being returned. Mike Kurt Lieber wrote:
I'm executing a query that returns some a UID and a company name. I'm using the following code to loop over that query result set:
       $result = mysql_query($myQuery) //$myQuery gets defined earlier
       $resultCount = mysql_num_rows($result);
       $companyID = array();
           while($query_data = mysql_fetch_row($result)) {
               $companyID[$query_data[0]] = $query_data[1];
               $companyID_string .= "," . $query_data[0];
               $companyName_string .= "," . $query_data[1];
           }
I've used $resultCount to verify the query is returning 4 rows. However, the while loop above is looping over the results twice -- $companyID_string and $companyName_string each have 8 values. Looking at the values shows that it's the 4 rows duplicated. What the heck am I doing wrong? --kurt


« previous php.general (#72339) next »