Function won't allow to be called more than once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Function won't allow to be called more than once

Post by Da P3s7 »

Code: Select all

function mysql_fetch_all($result) {
   $i = 0;
   for ($i=0; $i<mysql_num_rows($result); $i++) {
       $return[$i] = mysql_fetch_array($result);
   }
   return $return;
}
I have the above function which after calling to it once won't let me call to it again.
Any way to modify it so i can call it at least twice or possible infinite??

I get this when i call it a second time and print_r the result

Code: Select all

Array ( [0] => [1] => [2] => [3] => [4] => )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_data_seek() may be of interest.
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

umm.... not to be rude but that is a bit irrelevant, as the function works but i need it to call it more than once with arguments which are not the same......
OR are you suggesting that i use the mysql_data_seek() in the function ???.....
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Da P3s7 wrote:umm.... not to be rude but that is a bit irrelevant, as the function works but i need it to call it more than once with arguments which are not the same......
OR are you suggesting that i use the mysql_data_seek() in the function ???.....
Search user comments
http://php.net/mysql_fetch_array
...
One of the most common mistakes that people make with this function, when using it multiple times in one script, is that they forget to use the mysql_data_seek() function to reset the internal data pointer.
...
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

Sorry for bringing this up again...
I tried to use mysql_data_seek() to no avail....
The thing is... I don't parse the same query result, i parse a different result.
Although this does give me an idea:
What if i close the mysql connection and then reopen it?
Would that work?

P.S. To Feyd: :oops:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

function mysql_fetch_all($result, $opt = MYSQL_ASSOC) { 
    $return = array(); 
    mysql_data_seek($result, 0);
    while ($row = mysql_fetch_array($result, $opt)) {
        $return[] = $row;
    }
    return $return; 
}
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

Your function works just like mine... :(
It works for the first query but not for the second. :?
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

After extensive tries i havent figured out a way of using just one query.
mysql_data_seek() just doesnt seem to be working for me... for some reason...
Both by fathers briliant logic and a friend of mine's programming capapbilities (he works for the company that made the first video-game( pong)) couldn't sort out the mess.
Closing and reopening the connection doesn't work...
However, this friend did suggest a middle file. I'll explain:
He said "make the 2 queries in 2 different files and use the $_POST[] to get the info u need".
Would that work?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Just save the data to an array, why requery the db over and over again?

Code: Select all

$db = mysql_query('query', 'dblink');
$i = 0;

while ($data = mysql_fetch_assoc($db))
{
    $result[$i] = $data;
    $i++;
}

reset($result);
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

He's not requerying, he's only retrieving from the result resource.

Another option is to make a wrapper for the mysql_query function so you start with an array.

Code: Select all

function mysql_query_array($sql, $link, $opt = MYSQL_ASSOC)
{
    $data = mysql_query($sql, $link);
    $return = array();

    if (is_resource($data)) {
        while ($row = mysql_fetch_array($data, $opt)) {
            $return[] = $row;
        }

        return $return;
    } else {
        return $data;
    }
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Jenk wrote:He's not requerying, he's only retrieving from the result resource.
I was referring to mysql_fetch_array, I should have been more clear. That still communicates with MySQL right?
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

What i don't get is why does mysql_num_rows work while the function doesn't work...(@ jenk: your's doesn't work either 8O )
(I use the mysql_num_rows for pagination.) The mysql_num_rows returns the number of rows from the whole table and incredibly works... 8O :!: 8O
This gives you guys any ideas?

@ astions:
Well i need to do 2 different queries:
One is for the indexing system that manually increases the index. (OMG this post just gave me a huge idea)
And the second is to print out the data in the table.
Post Reply