MySqlTalk.com  

Go Back   MySqlTalk.com > Programming Languages and MySQL > PHP
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 03-08-2006, 07:56 PM   #1
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
Why can you only use fetch_array only once?

Here's some code. Why can't you do this while statement twice?

PHP Code:
$getbooks $DB_site->query("SELECT books.* FROM bookdb WHERE bookid>1000");
//FIRST WHILE STATEMENT
$comma="";
while (
$getbook $DB_site->fetch_array($getbooks))
{
  
$bookids.="$comma $getbook[bookid]";
  
$comma=",";
}
//Run some queries with the $bookids on another table.
//SNIP
while ($getbook $DB_site->fetch_array($getbooks))
{
  
//Do some stuff here, 
  //using an array created with the queries run with $bookids


Unfortunately, I ran a test and the second whil statement is blank. Here's the test:

PHP Code:
$comma="";
        while(
$getbook $DB_site->fetch_array($getbooks))
        {
            
$bookids.=" $comma ".$getbook[bookid];
            
$comma=",";
        }
        echo 
"here's the bookids test part 1: $bookids<br />";
        
$comma="";
        while(
$getbook $DB_site->fetch_array($getbooks))
        {
            
$bookids2.=" $comma ".$getbook[bookid];
            
$comma=",";
        }
        echo 
"here's the bookids test part 2: $bookids2<br />";
        exit;

here's the bookids test part 1: 450858 , 450678 , 450677 , 450676 , 450675 , 450674 , 450673 , 450671 , 450670 , 450614 , 450612 , 450531 , 450529 , 450528 , 450526 , 450525 , 450523 , 450521 , 450476 , 450454
here'
s the bookids test part 2
__________________
** Most misspellings intentional to combat spaham filterz**
Azkaban is offline   Fork this post Reply With Quote
Old 09-25-2006, 06:42 AM   #2
doc
Junior Member
 
Join Date: Sep 2006
Posts: 4 doc is on a distinguished road
Re: Why can you only use fetch_array only once?

mysql_fetch_array() returns an array that corresponds to the fetched row and moves the internal data pointer ahead and leaves it there. So repeating the process starts after your values and thus returns nothing.

It does seem odd though. Obviously you never need to repeat it - I assume you were just curious. I'd never spotted it in years of using php!
doc is offline   Fork this post Reply With Quote
Old 09-25-2006, 08:33 PM   #3
Administrator
Administrator
 
Join Date: Jun 2004
Posts: 383 Administrator is on a distinguished road
Re: Why can you only use fetch_array only once?

I've come across this requirement and found a way to reset the pointer. Can't remember the code offhand but there is a way to do it.
Administrator is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -4. The time now is 12:37 PM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".