查询数据库啊
function GetIPs(){
$conn=mysql_connect("localhost","root","123456");
if(!$conn){
die("连接失败".mysql_error());
}
mysql_select_db("test");
mysql_query("set names utf8");
$sql="select ip from user1 where ...";
$res=mysql_query($sql,$conn);
while($row=mysql_fetch_row($res)){
$ip=$row['ip'];
}
mysql_free_result($res);
mysql_close($conn);
return $ip;
}

解决方案 »

  1.   


    首先谢谢楼上的朋友! 我参考了下,数据库是a表字段是ip,这样写的代码,但是运行显示空白!不知道错在哪里!请指点!
    <?php
    function GetIPs(){
    require("conn.php");
        mysql_query("set names utf8");
        $sql="select * from a order by id desc limit 0,1";
        $res=mysql_query($sql,$conn);
        while($row=mysql_fetch_row($res)){
            $ip=$row['ip'];
        }
        mysql_free_result($res);
        mysql_close($conn);
        return $ip;
    }
    //远程城市数据返回点
    $filename = "http://ip.taobao.com/service/getIpInfo.php?ip=".GetIPs();
    //使用file_get_contents返回json数据,确认程序是否开启file_get_contents
    $json = json_decode(file_get_contents($filename));
    //转换编码,不然会乱码
    $city=iconv("utf-8","gb2312",$json->data->city);
    //打印城市
    echo $city;
    ?>
      

  2.   

    $filename = "http://ip.taobao.com/service/getIpInfo.php?ip=".GetIPs();var_dump(GetIPs());
      

  3.   


    不行  显示   NULL
      

  4.   

    不好意思..

    mysql_fetch_assoc
      

  5.   

    不好意思..

    mysql_fetch_row
    换成
    mysql_fetch_assoc
      

  6.   

    <?php
    function GetIPs(){
    require("conn.php");
        mysql_query("set names utf8");
        $sql="select * from a order by id desc limit 0,1";
        $res=mysql_query($sql,$conn);
        while($row=mysql_fetch_assoc($res)){
            $ip=$row['ip'];
        }
        mysql_free_result($res);
        mysql_close($conn);
        return $ip;
    }
    //远程城市数据返回点
    $filename = "http://ip.taobao.com/service/getIpInfo.php?ip=".GetIPs();
    //使用file_get_contents返回json数据,确认程序是否开启file_get_contents
    $json = json_decode(file_get_contents($filename));
    //转换编码,不然会乱码
    $city=iconv("utf-8","gb2312",$json->data->city);
    //打印城市
    echo $city;
    ?>
    是这样吗?但是还是出错!Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
      

  7.   

    查看是否选择了数据库mysql_select_db("test");
    再将你的sql语句放到mysql下执行,看是否报错
      

  8.   

    $res=mysql_query($sql,$conn) or die(mysql_error());   
    这样报什么错误
      

  9.   

    这有你们的!
    GetIPs 的本意是获取来访者的 ip
    然后根据来访者的 ip 查得其所在城市那么,如果 GetIPs 是从数据库ip字段来的话,至少得有一个查询依据吧?
    你们就想当然的去取一条记录,那能表示什么呢?
    且不说你取的那条记录恰好 ip 是空的,就算是有值又能说明什么呢?楼主不懂,描述的需求不清楚,那你们也不懂吗?