简体   繁体   中英

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). This is why you have to use:

$check= "SELECT * FROM ip WHERE ip = '{$_SERVER["REMOTE_ADDR"]}'";

我认为,如果您只需要计数,请使用COUNT()

$sql = "SELECT COUNT(*) FROM ip WHERE ip = '".$_SERVER["REMOTE_ADDR"]."'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM