0

So here's the code:

function procRestSearch($searchQuery){ 
		//Don't forget to have $searchquery defined before calling this function
		$intSearchCounter = 0; //Used to set the arrays
		$searchResult = mysql_query($searchQuery);
		while($searchRow = mysql_fetch_array($searchResult, MYSQL_ASSOC)){
			//Change/Add/Delete the variables below to match what data needs to be returned
			$strRest = array($intSearchCounter => array('restID' => $searchRow['rest_id'], 
				'restName' => $searchRow['rest_name'], 'restAddress' => $searchRow['rest_address'], 
				'restAddress2' => $searchRow['rest_address2'], 'restCity' => $searchRow['rest_city'], 
				'restState' => $searchRow['rest_state'], 'restZip' => $searchRow['rest_zip'], 
				'restCC' => $searchRow['rest_country_code'], 'restAC' => $searchRow['rest_area_code'], 
				'restPhone' => $searchRow['rest_phone_no'], 'restFAC' => $searchRow['rest_fax_no'], 
				'restManager' => $searchRow['rest_manager'], 'restAsManager' => $searchRow['rest_assistant_manager'], 
				'restWebsite' => $searchRow['rest_website']));
			
			$intSearchCounter++;
		}
		return $strRest;
	}

the problem is when I call this function I get a Notice: Undefined offset: 0 in... error. However, if I replace $intSearchCounter with a static value, such as just replacing it with 0, it works but then I only get the last result from the query returned. What am I doing wrong?

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by nav33n
0

Initialize the array outside the while loop. The array is getting over-written everytime it enters the loop. So you are getting only the last record.
Secondly, notices can be ignored. If you use variables without initializing them, you will get a notice. It is a good programming practice to initialize all your variables before using them. But sometimes, it gets a little annoying. You can disable notices by changing your php.ini file.
Search for error_reporting and uncomment this line.

error_reporting = E_ALL & ~E_NOTICE

Meaning, show all errors except notices.
Check this link to configure error reporting in your script.

Cheers,
Naveen

0

No good. I can't initialize the array outside of the while loop for none of the values will be set unless there's another way to initialize an array in php...

and i want the errors displayed so i know what's going on with my code. if i try to set the first key in the array with the variable $intSearchCounter, which increments by 1 after each run through of the loop, that's when i get the error and NO RESULTS RETURNED, just the error. Testing where my code went wrong, I replaced $intSearchCounter with 0 just to see what would happen and it only gave me the last result in the query which i fully expected. I just wanted to see where the code went wrong. Basically, it appears as if the array is ignoring $intSearchCounter but I don't know why this would happen.

0

What I meant was ,

function procRestSearch($searchQuery){ 
	//Don't forget to have $searchquery defined before calling this function
	$intSearchCounter = 0; //Used to set the arrays
	$searchResult = mysql_query($searchQuery);
	$strRest = array();
	while($searchRow = mysql_fetch_array($searchResult, MYSQL_ASSOC)){
		//Change/Add/Delete the variables below to match what data needs to be returned
		$strRest[$intSearchCounter] = array('restID' => $searchRow['rest_id'], 
			'restName' => $searchRow['rest_name'], 'restAddress' => $searchRow['rest_address'], 
			'restAddress2' => $searchRow['rest_address2'], 'restCity' => $searchRow['rest_city'], 
			'restState' => $searchRow['rest_state'], 'restZip' => $searchRow['rest_zip'], 
			'restCC' => $searchRow['rest_country_code'], 'restAC' => $searchRow['rest_area_code'], 
			'restPhone' => $searchRow['rest_phone_no'], 'restFAC' => $searchRow['rest_fax_no'], 
			'restManager' => $searchRow['rest_manager'], 'restAsManager' => $searchRow['rest_assistant_manager'], 
			'restWebsite' => $searchRow['rest_website']);		
		$intSearchCounter++;
	}
	return $strRest;
}

And secondly, using the above error reporting would still display the errors but not the notices. As I said in my previous post, if you like to fix the notices, initialize your variables, use $var instead of $var[something] and so on. (notices can be ignored).
Does it work for you ?

0

You are the man. I just didn't understand what you mean by initializing the array in the original reply but seeing it makes perfect sense. Thanks so much.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.