将MYSQL命令行查询转换为内联php查询

时间:2015-01-15 17:56:49

标签: php mysql

我几乎可以让MySQL在本地存储的maxmind数据库中进行geoip查找。说明我在使用命令行mysql>时一直在关注工作。检索记录的语法。如:

mysql> SELECT   glc.*
   FROM     geoip_blocks gbl
            JOIN geoip_locations glc
            ON       glc.glc_id = gbl.gbl_glc_id
   WHERE    gbl_block_start <= INET_ATON('149.156.1.4')
   ORDER BY gbl_block_start DESC
   LIMIT    1\G  

但是,我很难将其转换为php脚本中的mysql查询。

我试过了:

`Select glc.* from geoip_blocks gbl JOIN geoip_locations glc ON glc.glc_id = gbl.gbl_glc_id WHERE    gbl_block_start <= INET_ATON('149.156.1.4') ORDER BY gbl_block_start DESC LIMIT 1\G` 

但这不起作用。我确信这很容易解决,但我很难过。

2 个答案:

答案 0 :(得分:1)

mysql命令行有一个\G宏,但原始SQL中不存在这种情况。把它留下来。也没有必要包含;

答案 1 :(得分:0)

你的php脚本应该是这样的:

//set connection up
$mysql = new mysqli('mysql_server', 'mysql_user_name', 'mysql_password','mysql_DB');

//query
$sql = "SELECT glc.*
        FROM geoip_blocks gbl
        JOIN geoip_locations glc ON glc.glc_id = gbl.gbl_glc_id
        WHERE gbl_block_start <= INET_ATON('149.156.1.4')
        ORDER BY gbl_block_start DESC
        LIMIT 1";  

//run query & store result
$result = $mysql->query($sql);

//take a look at the results
if($result){
    while($row = $result->fetch_assoc()){

        var_dump($row);

    }
}