Search for Zip Code Band

Asked

Viewed 384 times

1

Good afternoon, I need to get a zip code from the zip line registered in the bank...

**banco de dados:**
cep_id | cidade | cep_de    | cep_ate
1      | jau    | 10000000  | 15000000



$cep = "15086210";

$stmt = $pdo->prepare("SELECT * FROM ceps WHERE :a BETWEEN cep_de AND cep_ate");                                    
$stmt->bindParam(':a', $cep);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);

if($result->cep_de == $cep):
 echo "ok";
else:
 echo "erro";
endif;

If the zip code is within the registered zip line it gives an 'ok", if it is not within the track it returns an "error".

How do I return the result?

  • 3

    WHERE cep_de = :a BETWEEN cep_de AND cep_ate this mixture of = with BETWEEN does not make much sense for what you want. If you want a zip code within a band enough WHERE :a BETWEEN cep_de AND cep_ate - These things are usually solved using examples from the manual itself: https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_between

  • @Bacco Where was my question not clear? Can you help me?

  • 1

    It’s clear, who said no? It was closed for typo, not for lack of clarity. According to the first comment (which already contains the solution) - it is a syntax error, I even indicated the manual with examples. Follow again the correct Where syntax to avoid misunderstandings: WHERE :a BETWEEN cep_de AND cep_ate

  • @Bacco understood... I did as you passed, but it is not returning the result. How do I do this?

  • 1

    Well, there are more problems, like this one in your code if($result->cep_de == $cep) - it would be good to take out these irrelevant parts and simply show the result that came. Testing DB return with "if" usually doesn’t make any sense

  • @Bacco but I need to give a return via ajax, if the cep that was typed is within the area that is served.

  • 1

    Then you’re mixing up a problem that has nothing to do with our conversation. First you need to learn how to do the basics of SQL, which is the first serious error of your post; when this works (no PHP or anything) there you go to the PDO question. When the PDO works, then you think of AJAX, otherwise it is impossible.

  • 1

    See SQL working perfectly here: http://sqlfiddle.com/#! 9/023946/1 - your next logical step would be to make it work on the PDO with the :a. As I said, remove ifs and simply show the result on the screen, it’s easier. print_r( $result );

  • @Bacco the PDO and all SQL is ok. I am grateful. Now I do not know how to return to me the "ok and the "error"

  • 1

    If came result is always OK, no? Error would be if came empty. Perhaps it would be better to return an array instead of an object, and simply count the amounts returned (zero would be the error maybe). But then I can not give more suggestions because it depends on how you will use the values.

  • @Bacco need that after making the check it returns me whether it is within the range we attend or not. There is no way to do this with "if"? as in the example I gave above?

  • Well, I have to finish here for today to do other things, but remember that your points already use the chat room "Stack Burst", accessible from the menu of the site. These hours are kind of empty, but the office hours always have people. There is a better space for things that do not fit well in the objective answer objective question format

  • between will only return if it is in the DB tracks. If you want a specific one you need to test if the ID is met. Now, if DB only has the tracks you answer is what I said before - if any result comes it’s always OK

  • @Bacco then caught by id.... I will test... vlw

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.