Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am having a problem with simple s

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
I am having a problem with simple select statement

When I run this statement in my PHP program.. no result..

$sql= "select * from zipcodes where zip between $pluszip and $minuszip";

But when I run the below statement I get the results..

$sql= "select * from zipcodes where zip between 12345 and 12456";

I tried this one also, but no result
$sql= "select * from zipcodes where zip between '$pluszip' and '$minuszip'";

I tried echoing pluszip and minuszip and i am getting the values printed..I am having trouble with passing it on to the select statement.

regards
MP
 
Try something like:

$sql= "select * from zipcodes where zip between ".$pluszip." and ".$minuszip.";";
 
Below is my PHP code

<html>
<body>

<?php
// getting the values from the HTML form

$zipcode = $_POST['zipcode'];
$radius = $_POST['radius'];
$pluszip= $zipcode + $radius;
$minuszip= $zipcode - $radius;

echo $pluszip;
echo $minuszip;

$conn=mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;root&quot;);
mysql_select_db(&quot;storelocator&quot;,$conn);
//$sql= &quot;select * from zipcodes where zip between '$pluszip' and '$minuszip'&quot;;
//$sql= &quot;select * from zipcodes where zip between 48000 and 48185&quot;;
// $sql= &quot;select * from zipcodes where zip between '&quot;.$pluszip.&quot;' and '&quot;.$minuszip.&quot;'&quot;;
$sql= &quot;select * from zipcodes where zip between &quot;.$pluszip.&quot; and &quot;.$minuszip.&quot;;&quot;;

$result=mysql_query($sql,$conn);
$num=mysql_num_rows($result);

if ($num == 0)
{
echo &quot;<b>&quot;;
echo &quot;No Entries in the database&quot;;
echo &quot;</b>&quot;;
exit();
}

while ($row=mysql_fetch_array($result))
{
$a=$row[&quot;name&quot;];
echo &quot;<br>&quot;;
echo &quot;<b>&quot;;
echo $a;
echo &quot;</b>&quot;;
echo &quot;<br>&quot;;
}

?>
</body>
</html>

I am getting the values of pluszip and minuszip printed when i echo them..

My sql table

DB-storelocator
Table-zipcodes

zip name
48185 test1
48188 test2

any other suggestions pls..

regards
MP
 
Take a look at my FAQ.

Verify your query looks like it should.
Test the query, as produced by your code, by copy-and-pasting it into your preferred MySQL admin app.
Use error-trapping in your code. MySQL could be trying to tell you what the error is, but your code doesn't trap it.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
hello sleipnir214
thx for responding..
I have now addted the error handler code and still same result and i am not receving error which shows my query is correct..

regards
MP
 
I solved the problem finally..
A silly mistake I made..

$sql= &quot;select * from zipcodes where zip between $minuszip and $pluszip&quot;;

Just swapped the pluszip and minuszip..I was always trying with &quot;select between pluszip and minuszip&quot; Thats where the script got confused.

Thanks for your responses...

regards
MP
 
Something to consider:-

Not knowing how you obtain these values, I will guess you have a form that a user inputs the zip codes into, you should look into either a validation script to ensure value a is always less than value b or you should make an if clause like so:
Code:
if ($minuszip <= $pluszip) {
   $sql= &quot;select * from zipcodes where zip between $minuszip and $pluszip&quot;;
}
elseif ($minuszip > $pluszip) {
   $sql= &quot;select * from zipcodes where zip between $pluszip and $minuszip&quot;;
}
else {
   echo &quot;Error - a unexpected value was detected.&quot;;
};
HTH
Charlie
 
Doh, sorry, just read the source code you posted a number of posts back and see that you are using a form, but I would still consider some form of error trapping in the future that would prevent this issue hitting you again.

GL and sorry for my ignorance!
Charlie
 
Hello Again
I am trying to implement a Zip Code Locator Program for one of the websites as you can see the source code.
I am doing it in a different way of just getting the user's input of the zip code plus the radius miles and grabbing it from my database..

When I browsed thru the net I see lot of people talking about latitude and longitudes etc..Could anyone educate me on this stuff..

regards
MP
 
Sorry for my above post..

regards
MP
 
Yes the long/lat post is most def out of place on this thread; however to answer it anyway, as I work with GIS Systems... you don't need the long/lat unless you are using a GPS or some pin pointing system like it.

It don't make sence, the zip code is for a region, long/lat is to pinpoint. I figure you are doing a radius from the zip code boundry - not your exact location!

If the later is the case then you had better stock up on pain killers for many headaches are coming your way, as your task is about to get extreemly dificult!

GL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top