Jump to content

[SOLVED] Query working solo..Not in a while loop?!


AbydosGater

Recommended Posts

Hey Guys,

Im redoing my config class for my site, The __construct() is basicly just selects each row of the config table and saves it to the $this->data[] array..

function __construct(){
	global $sql;
	$temp = $sql->_query(" SELECT * FROM sb_config ORDER BY `sb_config`.`key` ");
	while($temp = $sql->_fetch_assoc($temp)){
		$this->data[] = $temp;
	}
	define('URL', $this->data['domain'].$this->data['path']);
}

The database is basicly just:  Key -- Value.. IE: domain -- localhost

 

Now if i run that code.. I get a "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"..

But if i take out the loop and use:

function __construct(){
	global $sql;
	$temp = $sql->_query(" SELECT * FROM sb_config ORDER BY `sb_config`.`key` ");
	$temp = $sql->_fetch_assoc($temp);
        print_r($temp);
        }

 

That works fine.. selects the first row..and it is a valid resource.. But why is it not working with the loop?

 

Andy

Change $temp to a different variable for your while, eg:

	function __construct(){
	global $sql;
	$temp = $sql->_query(" SELECT * FROM sb_config ORDER BY `sb_config`.`key` ");
	while($row = $sql->_fetch_assoc($temp)){
		$this->data[] = $row;
	}
	define('URL', $this->data['domain'].$this->data['path']);
}

 

When you use $temp = $sql->_fetch_assoc($temp). You are overwriting the temp variable which then losses the result resource for the query and thus you are getting the error you getting.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.