Recursive call to database
Posted: Sat Apr 12, 2008 9:09 am
I am trying to write a recursive function that call a database. I have a table of zipcodes and their latitudes and attitudes. In addition I have a store procedure that accept zipcode and radius, and return the near by zipcodes. I need a function that will return only 7 zipcodes. To achieve this I trying to write recursive function that will change the radius according to the number of zipcodes return. But, I could not make the function work. Below is the code and the error I got from PHP. I will appreciate any suggestions.
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\workspace\project3\GetZipcodes.php on line 32
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\workspace\project3\GetZipcodes.php on line 41
Code: Select all
//Connect to database
$conn = mysql_connect($DBHOST, $DBUSER, $DBPASS, 0, 65536);
if(!$conn){
exit('Error connecting to databse'. mysql_error());
}
//Open database
$db_select = mysql_select_db($DBNAME);
if(!$db_select){
exit('Error opening to databse: '. mysql_error());
}
//Set radius variable
$radius = 10;
$zipcode = '02138';
GetNearZipCodes($zipcode, $radius);
function GetNearZipCodes($zip, $rad){
global $conn;
echo 'functions param: '.$zip.' '.$rad;
$zip = mysql_real_escape_string($zip);
$result = mysql_query("CALL GetNearbyZipCodes('{$zip}', {$rad})", $conn);
if(isset($result)){
$num_rows = mysql_num_rows($result);
if($num_rows > 7){
//reduce the size of radius
--$rad;
GetNearZipCodes($zip, $rad);
}else{
while ($row = mysql_fetch_assoc($result)){
echo '<br />'.$row[zipcode];
}//end while
}//end else
}
}
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\workspace\project3\GetZipcodes.php on line 41