mysql 表字段 - 资源 ID #7 [英] mysql table fields - Resource id #7
问题描述
我在数据库中有下表:
id, username, email, old_ip, current_ip
每次用户登录时,我都希望他们当前的 ip 存储在 current_ip 中.但在此之前我想保存他们的 old_ip(从上次登录开始)所以我想我可以在将新数据复制到 current_ip 之前将 current_ip 中的内容移动到 old_ip.
Everytime the user logs in, I want their current ip to be stored in current_ip. But before that I want to save their old_ip (from last login) so I thought I could move what is in current_ip to old_ip before copying the new data to current_ip.
我在登录时写了这个脚本:
I wrote this script when logging in:
........
$sql = mysql_query("SELECT * FROM members WHERE email='$email' AND password='$password'");
$login_check = mysql_num_rows($sql);
if($login_check > 0){
while($row = mysql_fetch_array($sql)){
// Get member ID into a session variable
$id = $row["id"];
session_register('id');
$_SESSION['id'] = $id;
// Get member username into a session variable
$username = $row["username"];
session_register('username');
$_SESSION['username'] = $username;
// IP getting
$old_ip = mysql_query("SELECT current_ip FROM members WHERE id='$id'");
$result_old_ip = $old_ip;
$sql_setoldip = mysql_query("UPDATE members SET old_ip='".$result_old_ip."' WHERE id='$id'");
$getip = getenv("REMOTE_ADDR");
$current_ip = $getip;
// Update ip in database
mysql_query("UPDATE members SET current_ip='".$current_ip."' WHERE id='$id'");
}
我在 old_ip 字段中得到的结果是Resource id #7" 有什么帮助吗?
The result I get in the old_ip feild is "Resource id #7" Any help please ?
推荐答案
mysql_query()
语句返回一个指向结果集的资源指针,而不是数据本身.您需要使用 mysql_fetch_array()
来检索表中的实际数据.
mysql_query()
statement returns a resource pointer to the result set, not the data itself. You'll need to use mysql_fetch_array()
in order to retrieve the actual data in the table.
像这样:
$old_ip = mysql_query("SELECT current_ip FROM members WHERE id='$id'");
$row = mysql_fetch_array($old_ip);
$result_old_ip = $row['current_ip'];
这将解决您的问题.
希望这会有所帮助.
这篇关于mysql 表字段 - 资源 ID #7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!