简体   繁体   中英

PHP PDO usage with MySQL BETWEEN and INET_ATON()

I started a PHP side project a while back, and recently started cleaning up all the code and updating it. Part of that is moving things to start using PDO for DB queries. In one particular query I have a WHERE clause statement that uses the BETWEEN clause along with the INET_ATON() function. So far I have not been able to get this to work properly and would appreciate if anyone can point out whether or not it will even work.

I found this question which leads me to believe that the BETWEEN statement is not having any issues, but I fear it might be the combination with the function causing the issue.

Here is what that portion of my query looks like:

SELECT foo, bar FROM table JOIN more_table 
WHERE item1 = :value AND ip_address BETWEEN INET_ATON(:ipstart) AND INET_ATON(:ipend)

The actual query is quite a bit larger than that, but for basics this works. To replace the tokens with the actual values I have the following foreach loop set up (the var_dump is just there so i can verify the correct values are getting passed in, which they are):

foreach($p as $k=>$v) {
    echo var_dump($k, $v);
    if(is_int($v)){
        $querySubmit->bindValue($k, $v, PDO::PARAM_INT);
    } else {
        $querySubmit->bindValue($k, $v, PDO::PARAM_STR);
    }
}

If anyone knows whether PDO::Prepare and PDO::bindValue have any limitations with a query set up to use BETWEEN and INET_ATON() , I would greatly appreciate knowing!

Your parameter for bindValue() is $k it should be :value

foreach($p as $k=>$v) {
    echo var_dump($k, $v);
    if(is_int($v)){
        $querySubmit->bindValue(:value, $v, PDO::PARAM_INT);
    } else {
        $querySubmit->bindValue(:value, $v, PDO::PARAM_STR);
    }
}

Also are you binding :ipstart and :ipend ?

The issue ended up being with a separate section of code. The values being passed into the SQL query were correct, and the bindValue operation was working successfully.

To anyone who reviews this question in the future, who believes they are having a similar problem, if set up correctly there should be no issues with using the INET_ATON/NTOA() functions with BETWEEN .

I believe the first comment, left by @MarcB, was probably the best answer to this actually, "PDO has absolutely NO knowledge of MySQL functions. as long as the MySQL parser doesn't reject the query string, PDO will happily shove in whatever you want, wherever you want it."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM