简体   繁体   中英

Check in mysql if entry exists and not insert with php

I have got a problem with testing out if a string exists in my database!

This is what I've got so far:

$db = mysqli_connect("localhost", "database", "secret", "user");
if(!$db)
{
exit("Error: ".mysqli_connect_error());
}
$search = 'SELECT * FROM table WHERE ip = $client_ip';
$result = mysqli_query($db, $search);



while($row = mysqli_fetch_object($result)) {
        if (isset($row->blocked)) {
            echo 'You are blocked!';
        } else {
            echo 'You are not blocked!';
        }

But this won't work for me. $client_ip is defined correctly before.

您需要将$client_ip用引号引起来,因为它是一个字符串:

$search = "SELECT * FROM table WHERE ip = '$client_ip'";

试试这个:

$search = "SELECT * FROM table WHERE ip = '{$client_ip}'";

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