0

I have searched about problem but i can't solve this code, I see this error :

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\EasyPHP\www\test\index.php on line 13

and this is my PHP codes :

<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
function ip_exists($ip) {
    $con = mysqli_connect("localhost","root","","ok");
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    global $user_ip;
    $query = "SELECT ip FROM hits_ip WHERE ip = $user_ip";
    $query_run = mysqli_query($con,$query);
    $query_num_rows = mysqli_num_rows($query_run);

    if ($query_num_rows==0) {
        return false ; 
    }
    else if($query_num_rows>=1) {
        return true ;
    }
}
if (ip_exists($user_ip)) {
    echo "EXIST";
}
else {
echo "NO ex";
}
?> 

What is the problem and what should i do ?

I want to use mysqli .

  • possible duplicate of [PHP & MySQL: mysqli\_num\_rows() expects parameter 1 to be mysqli\_result, boolean given](http://stackoverflow.com/questions/2546314/php-mysql-mysqli-num-rows-expects-parameter-1-to-be-mysqli-result-boolean) – Carsten Mar 19 '14 at 09:37
  • 2
    Your query seems to be failing. Can you do a `echo mysqli_error();` after the query execution? – AyB Mar 19 '14 at 09:38
  • @Carsten so what code should i use? – user3394468 Mar 19 '14 at 09:38
  • 2
    People should learn how to debug before posting questions – Royal Bg Mar 19 '14 at 09:42

4 Answers4

3

You have an error in your SQL query. That's why mysqli_query() doesn't return a mysqli_result, but a bool instead. Run mysqli_error($con). That will give you a description of the error. In this case, you are missing quotes around the $user_ip variable in your query. This should correct it:

$query = "SELECT ip FROM hits_ip WHERE ip = '$user_ip'";
Carsten
  • 17,991
  • 4
  • 48
  • 53
2

Try:

$query = "SELECT ip FROM hits_ip WHERE ip = '" . $user_ip . '"";

Missing single quotes while searching ip, though your query seems to be odd, because you are retrieving exactly the same ip which you're searching. In this case why don't you just use $user_ip instead !?

nanobash
  • 5,419
  • 7
  • 38
  • 56
1

You should remove global $user_ip;

The function takes the global $user_ip instead of the parameter passed in the function header

also imo, with or without quotes should work

creavery
  • 134
  • 1
  • 8
1

you have a problem into your query string ..

use mysqli_error() function to display error in your code . link

$query = "SELECT ip FROM hits_ip WHERE ip = $user_ip";
echo mysqli_error();
$query_run = mysqli_query($con,$query);

change your query string with

$query = "SELECT ip FROM hits_ip WHERE ip =". $user_ip;
i'm PosSible
  • 1,373
  • 2
  • 11
  • 30