我如何查询此信息

时间:2016-01-23 08:49:04

标签: php sql pdo

我有这个脚本,他就是我的数据库的所有表格,我知道如何才能看到表格" CITY_NAME"我的DB。

我非常感谢能帮助我的人

<?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 个答案:

答案 0 :(得分:0)

如果我理解正确,我认为您只想查看表格中的city_name列。只需在选择查询中使用city_name代替*,如下所示。 SQL101

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