Page 1 of 1
db fetch loop problem
Posted: Wed Nov 23, 2005 4:47 am
by jurriemcflurrie
Hello here I am again with a small problem. I have the following:
Example db class
Code: Select all
class db {
var $result;
function fetch_query($sql) {
$this->query($sql);
while($row = $this->fetch()) {
$return[] = $row;
}
return $return;
}
function query($sql) {
$this->result = mysql_query($sql)
or die(mysql_error());
}
function fetch() {
$row = mysql_fetch_assoc($this->result)
or die(mysql_error());
return $row;
}
}
Get data example:
Code: Select all
$db = new db();
print_r($db->fetch_query("SELECT * FROM table"));
... it doesn't work. I get no results, and no errors. Can't figure out why :S
Posted: Wed Nov 23, 2005 5:03 am
by s.dot
Code: Select all
function query($sql) {
$this->result = mysql_query($sql)
or die(mysql_error());
}
should you be returning a value from this function? such as return $result
Posted: Wed Nov 23, 2005 5:18 am
by jurriemcflurrie
I don't think so? Thats where the fetch is for.
right?
Posted: Wed Nov 23, 2005 5:48 am
by Jenk
I haven't been able to ascertain why you are not receiving _any_ rows, but your fetch method will need a static variable to define which row to return, else it will return the same (first) row everytime it is called in the fetch_query while loop.
Either include a static variable for the row counter, e.g.:
Code: Select all
<?php
function fetch() {
static $i = 0;
if (!mysql_data_seek($this->result, $i)) {
unset($i);
return false;
} else {
$row = mysql_fetch_assoc($this->result)
or die(mysql_error());
return $row;
}
}
?>
Or restructure your methods into one method with the optional arg to fetch

Posted: Wed Nov 23, 2005 6:21 am
by jurriemcflurrie
Jenk:
The function you gave gives an endless loop (I don't know what mysql_data_seek() exactly does, I'll study that now). Anyways, the function I suggested gives diffirent results every time I call it from outside the class in a loop so I don't think that's the problem.
I think that php have a problem in assigning the row data to the $return[] var.
With error_reporting(E_ALL) set I don't get any errors. And I don't get ANY output. Whole page goes blank.

Posted: Wed Nov 23, 2005 6:28 am
by Jenk
My apologies, I made a very silly mistake:
Code: Select all
<?php
function fetch() {
static $i = 0;
if (!mysql_data_seek($this->result, $i)) {
unset($i);
return false;
} else {
$row = mysql_fetch_assoc($this->result)
or die(mysql_error());
$i++;
return $row;
}
}
?>
AHA!
I've just clicked.. your loop is also an infinite loop, following from what I mentioned earlier about the always returning the first row, that is why! The loop never breaks!
I must need some more coffee!
Posted: Wed Nov 23, 2005 6:47 am
by jurriemcflurrie
It works!
Only thing I added was an @ before mysql_data_seek() wich gave me a warning (on the last row I think).
Thanks (again) !
Posted: Wed Nov 23, 2005 8:25 am
by jurriemcflurrie
It didn't worked correctly. It returned one row to few.
Now it's this:
Code: Select all
function fetch() {
if($i > $this->num()) {
return false;
}
else {
return mysql_fetch_assoc($this->result);
$i++;
}
}
and it works for me. It doesn't return the same data, so every thing works ok!
