Page 1 of 1
Function won't allow to be called more than once
Posted: Sun Jul 23, 2006 12:35 pm
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] => )
Posted: Sun Jul 23, 2006 12:39 pm
by feyd
mysql_data_seek() may be of interest.
Posted: Sun Jul 23, 2006 2:11 pm
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 ???.....
Posted: Sun Jul 23, 2006 2:30 pm
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.
...
Posted: Fri Jul 28, 2006 3:33 am
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:

Posted: Fri Jul 28, 2006 3:56 am
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;
}
Posted: Fri Jul 28, 2006 4:05 am
by Da P3s7
Your function works just like mine...
It works for the first query but not for the second.

Posted: Thu Aug 03, 2006 10:35 am
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?
Posted: Thu Aug 03, 2006 10:44 am
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);
Posted: Thu Aug 03, 2006 11:00 am
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;
}
}
Posted: Thu Aug 03, 2006 11:09 am
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?
Posted: Sat Aug 05, 2006 4:25 pm
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

)
(I use the mysql_num_rows for pagination.) The mysql_num_rows returns the number of rows from the whole table and incredibly works...
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.