[英]How to check if ip already exists in MySQL with PHP?
This is my code, but it's not working; 这是我的代码,但是没有用; what's wrong?
怎么了?
$ip = $_SERVER["REMOTE_ADDR"];
$con=mysqli_connect("DBHOST","DBUSER","DBPASS","DBNAME");
$check= 'SELECT * FROM ip WHERE ip = '.$_SERVER["REMOTE_ADDR"];
$rs = $con->query($check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
$row_cnt = mysqli_num_rows($rs);
if( $row_cnt > 0 ) echo $row_cnt.' Records found';
The ip is not a number, it is a text (or varchar). ip不是数字,而是文本(或varchar)。 This is why you have to use:
这就是为什么您必须使用:
$check= "SELECT * FROM ip WHERE ip = '{$_SERVER["REMOTE_ADDR"]}'"; $ check =“ SELECT * FROM ip WHERE ip ='{$ _SERVER [” REMOTE_ADDR“]}''”;
我认为,如果您只需要计数,请使用COUNT()
$sql = "SELECT COUNT(*) FROM ip WHERE ip = '".$_SERVER["REMOTE_ADDR"]."'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.