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?

Recommended Answers

All 5 Replies

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

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.

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 ?

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.

Ah! All's well. :) Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.