Closed Thread Icon

Topic awaiting preservation: Beating a dead horse: ZIP code radius search (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=24189" title="Pages that link to Topic awaiting preservation: Beating a dead horse: ZIP code radius search (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Beating a dead horse: ZIP code radius search <span class="small">(Page 1 of 1)</span>\

 
mreish
Nervous Wreck (II) Inmate

From: Ward 1
Insane since: Nov 2004

posted posted 11-25-2004 18:49

I stumbled into these forum via a Google search for a project I'm working on. The idea is that someone can type in their zip code and get a list of restaurants within X number of miles. There's a thread on it http://www.ozoneasylum.com/21687 here and a GREAT FAQ here http://www.ozoneasylum.com/21781 for reference.

The thread ends with Pugzly saying there was a problem with the posted code but doesn't explain what it is. I'm a PHP/MySQL newbie and the syntax looks okay to me.

Can anyone explain what the problem is with the code provided in the FAQ?

LansingFood.com

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 11-25-2004 19:40

I think the issue was the while loop if there are no results.

Hmm... perhaps Pugz could post the code he ended up using.



.:[ Never resist a perfect moment ]:.

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 11-25-2004 20:27

Hi there, I'm the guilty one to the code in the FAQ...

What Pugzly ran into was not getting a valid result from the query ( $result = mysql_query($sql,$connection); ) as he entered the while loop after it:

code:
function doQueryResult($sql){

Global $connection;

hookUpDb();

if($sql != ""){

$result = mysql_query($sql,$connection);

$item = array();

while($item = mysql_fetch_array($result)){

$items[] = $item;

}

return $items;

mysql_free_result($result);

} else {

print("Need correctly formed SQL that returns a resultset.");

}

}


That can be corrected by adding an if($result){ do the loop } around the loop.
It shouldn't be a problem though if you have a valid sql and connection.
Other than that it should work ok.

You could add some checks to see if you are getting an answer if you are testing for a zip that doesn't exist. The example doesn't do that.
/Dan

{cell 260} {Blog}
-{ ?Computer games don?t affect kids; I mean if Pac-Man affected us as kids, we?d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.? (Kristian Wilson, Nintendo, Inc, 1989.) }-

(Edited by DmS on 11-25-2004 23:00)

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 11-25-2004 20:40

DmS: For clarity reasons, you should edit your post, 'coz UBB tags are not allowed inside the CODE UBB tag and you put a B UBB tag around the line:

code:
$result = mysql_query($sql,$connection);



mreish
Nervous Wreck (II) Inmate

From: Ward 1
Insane since: Nov 2004

posted posted 11-25-2004 22:26

Sorry guys but it doesn't work. The line in question was in the faq just the way it's presented here.

code:
$result = mysql_query($sql,$connection);


Here's the error I'm getting:

quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/lfc/public_html/calu.php on line 87



Line 87 reads:

code:
while($item = mysql_fetch_array($result)){


That makes me think it's in the query but I'm frustated with ignorance.

Yummy Places to Eat: LansingFood.com

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 11-25-2004 22:30

Just take this line

code:
while($item = mysql_fetch_array($result)){
$items[] = $item;
}



and wrap it like so:

code:
if ($result) {
while($item = mysql_fetch_array($result)){
$items[] = $item;
}
}



The issue is that if there is no result set then mysql_fetch_array will fail.

(hmm... php has try/catch now right? Gonna have to use that more)



.:[ Never resist a perfect moment ]:.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 11-25-2004 22:40

yeah, php 5 has that. Don't ask me how slow it is, though.
(Exception handling in most languages is rather slow compared to the rest of said languages).

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 11-25-2004 23:06

Ok, I've edited the post as poi suggested.

I think the error most likely comes from the connection being bad or nonexistent,
try to add this as well together with the part bitdamaged suggested:

code:
$result = mysql_query($sql,$connection) or die(mysql_error());



That should give you an error message that might give you a hint on where the problem is.
(you have changed the user/password/db parts in the hookUpDb() right?)

/Dan

{cell 260} {Blog}
-{ ?Computer games don?t affect kids; I mean if Pac-Man affected us as kids, we?d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.? (Kristian Wilson, Nintendo, Inc, 1989.) }-

« BackwardsOnwards »

Show Forum Drop Down Menu