[英]cannot compare ip address in wordpress php
I have created a custom table to store ip and post id 我创建了一个自定义表来存储ip和post id
$wpdb->insert('wp_firstvisit',
array('IP' => $_SERVER['REMOTE_ADDR'],
'post_id'=>$post_id ),
array('%s','%d')
);
when I try to get the result using 当我尝试使用
$ip = $_SERVER['REMOTE_ADDR'];
$result = $wpdb->get_row("SELECT * FROM wp_firstvisit WHERE post_id =$post_id AND ip=$ip");
I don't get any rows 我什么也没有
when 什么时候
AND ip=$ip
is removed I get a row 被删除我得到一行
That's not going to work, period. 那个时期,那行不通。 an IP address is not a number.
IP地址不是数字。 It's a string.
这是一个字符串。 You're doing what amounts to
你在做什么
SELECT ... ip = 127.0.0.1
Since that's not a valid number, and it's an unquoted string, you WILL get a syntax error at the sql level: 由于这不是一个有效的数字,并且是一个未加引号的字符串,因此您将在sql级别收到语法错误:
mysql> select 'foo' = 127.0.0.1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '.0.1' at line 1
^^^^^^
Strings MUST be quoted: 字符串必须用引号引起来:
SELECT ... ip = '127.0.0.1';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.