0

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.

4
  • The client IP address is notoriously unreliable. Be aware that you're likely to catch people you don't intend to and fail to catch some that you do. Commented Nov 5, 2014 at 2:15
  • you also need to check the mysqli_num_rows($result) to see number of return result before going ahead Commented Nov 5, 2014 at 2:18
  • @Saqueib before checking rows, we should check whether $result is false, as a failed query will return false by mysqli_query() Commented Nov 5, 2014 at 2:33
  • where $client_ip came from? Commented Nov 5, 2014 at 2:44

2 Answers 2

3

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

$search = "SELECT * FROM table WHERE ip = '$client_ip'";
Sign up to request clarification or add additional context in comments.

5 Comments

You need to call mysqli_error() to see what error mysql is reporting as that is correct syntax.
You can echo $search to find out the resultant SQL .
not a php dev, but i belive it needs to be: $search = "SELECT * FROM table WHERE ip = '" + $client_ip + "'";
@JordanFryar + is invalid character of concatination in php, use dot(.)
@JordanFryar $search = "SELECT * FROM table WHERE ip = '" . $client_ip . "'"; -- in php dot(.) is used for concatenation not plus(+)
0

try this one :

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

1 Comment

Please add some explanation as well to make it more clear what is the fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.