2015-01-15 82 views
0

我幾乎在那裏讓MySQL在本地存儲的maxmind數據庫中執行geoip查找。說明使用命令行mysql>語法檢索記錄時,我一直在繼續工作。如:將MYSQL命令行查詢轉換爲內嵌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 

但是,我很難將其翻譯成一個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` 

但是,這是行不通的。我確信這是一個簡單的解決方案,但我很難過。

回答

1

mysql命令行有一個\G宏,但在原始SQL中不存在這種情況。不要說了。沒有必要包含;

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); 

    } 
} 
+0

太好了!感謝您的反饋,是的,所有的工作! – jeffschips 2015-01-16 01:27:57