Jonas Feeder
Jonas Feeder

Reputation: 1

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.

Upvotes: 0

Views: 78

Answers (2)

Yassine
Yassine

Reputation: 35

try this one :

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

Upvotes: 0

John Conde
John Conde

Reputation: 219894

You need to wrap $client_ip in quotes as it is a string:

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

Upvotes: 3

Related Questions