2016-01-23 88 views
0

我得到了這個腳本,他是我返回我的數據庫的所有表,就像知道我怎麼只看到表「CITY_NAME」我的數據庫。如何查詢此信息

我很欣賞的人誰可以幫我

<?php 

//Configure the MySQL conwection 
$host = 'localhost';4 
$user = 'root'; 
$password = ''; 
$database = 'ip2location'; 
$table_name = 'ip2location_db3'; 

//Get the visitor IP address 
$ip = $_SERVER['REMOTE_ADDR']; 

//In case you are testing locally with 127.0.0.1, 
//you can uncomment the below line to assign the IP address 
//to 8.8.8.8 (or whatever) for your testing. 
$ip = '8.8.8.8'; 

try{ 
    //Create and perform the SQL query using the PDO 
    $db = new PDO('mysql:host=' . $host . ';dbname=' . $database . ';charset=utf8', $user, $password); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $st = $db->prepare('SELECT * FROM `' . $table_name . '` WHERE INET_ATON(:ip) <= ip_to LIMIT 1'); 
    $st->execute(array(':ip'=>$ip)); 

    $row = $st->fetch(PDO::FETCH_ASSOC); 



    //Print out the result 
    var_dump($row); 
} 
catch(PDOException $e) { 
    echo $e->getMessage(); 
} 


?> 
+1

我建議你開始閱讀對sql語言的基本介紹。然後你可以自己輕鬆回答你的問題。 – arkascha

+0

'CITY_NAME'是您的餐桌名稱或列名稱 – urfusion

+0

您是對的。這是專欄 –

回答

0

如果我理解正確的話,我想你想從你的表只查看city_name列。只需在下面的選擇查詢中使用city_name而不是*。 SQL101

'SELECT city_name FROM `' . $table_name . '` WHERE INET_ATON(:ip) <= ip_to LIMIT 1'