查询数据库啊
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;
}
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;
}
解决方案 »
- 一句代码不太明白
- php 泛解析 二级域名 session不同步的问题求角
- 一个JS刷新问题
- php中有类似于asp.net中的treeview控件吗?
- PHP查询分页问题 急~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- <? if(!isset($_SESSION['login_id'])){session_start();} ?>
- 这是什么问题造成的?真是奇怪了!!!!
- 一个文本文档中,有上万字,我不想一下全部读出来,那样速度太慢,如何实现“第一页 第二页 第三页 第四页 第五页”这样的效果哪
- pear db 的问题
- 高手帮忙阿
- php网站生成HTML速度问题
- 求助,厉害的来!
首先谢谢楼上的朋友! 我参考了下,数据库是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;
?>
不行 显示 NULL
把
mysql_fetch_assoc
把
mysql_fetch_row
换成
mysql_fetch_assoc
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
再将你的sql语句放到mysql下执行,看是否报错
这样报什么错误
GetIPs 的本意是获取来访者的 ip
然后根据来访者的 ip 查得其所在城市那么,如果 GetIPs 是从数据库ip字段来的话,至少得有一个查询依据吧?
你们就想当然的去取一条记录,那能表示什么呢?
且不说你取的那条记录恰好 ip 是空的,就算是有值又能说明什么呢?楼主不懂,描述的需求不清楚,那你们也不懂吗?