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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.