db fetch loop problem

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
jurriemcflurrie
Forum Commoner
Posts: 61
Joined: Wed Jul 06, 2005 7:14 am
Location: Den Haag, the Netherlands

db fetch loop problem

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
jurriemcflurrie
Forum Commoner
Posts: 61
Joined: Wed Jul 06, 2005 7:14 am
Location: Den Haag, the Netherlands

Post by jurriemcflurrie »

I don't think so? Thats where the fetch is for.

right?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
jurriemcflurrie
Forum Commoner
Posts: 61
Joined: Wed Jul 06, 2005 7:14 am
Location: Den Haag, the Netherlands

Post 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.

:|
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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!
User avatar
jurriemcflurrie
Forum Commoner
Posts: 61
Joined: Wed Jul 06, 2005 7:14 am
Location: Den Haag, the Netherlands

Post 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) !
User avatar
jurriemcflurrie
Forum Commoner
Posts: 61
Joined: Wed Jul 06, 2005 7:14 am
Location: Den Haag, the Netherlands

Post 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! :)
Post Reply