0

Please see I already checked old threads but did not help.

Here is my code, seems ok but gives error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

code:

function getPnr()
{
    $con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        return;
    }
    $pnr = mt_rand(1111111111, 99999999999);                
    $result = mysqli_query($con,"SELECT pnr from tbl_user where pnr = '".$pnr."'");
    if(mysqli_num_rows($result)>0)
        echo "emtpy";//getPnr();
    else
        echo $pnr;
}

what's wrong here?

UPDATED code:

function getPnr()

{

$con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    return;
}
$pnr = mt_rand(1111111111, 99999999999);                
$result = mysqli_query($con,"SELECT user_pnr from tbl_user where user_pnr = '".$pnr."'") or die(mysqli_error($con);
if(mysqli_num_rows($result)>0)
    echo "emtpy";//getPnr();
else
    echo $pnr;

return; }

new error:

Parse error: syntax error, unexpected ';' in F:\wamp\www\safari\REST_APIs_for_RedBus\main.php on line 104

  • 1
    It means your query didn't get executed successfully. Print the raw query and try executing it in phpMyAdmin or similar. Does it work? – Amal Murali Mar 08 '14 at 17:42
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – CBroe Mar 08 '14 at 17:52
  • Possible duplicate of [take your pick](http://stackoverflow.com/search?q=expects+parameter+1+resource+boolean+given) (1,145 results). – The Blue Dog Mar 08 '14 at 17:57

1 Answers1

0

This means your sql query failed to execute. Try to add error handling with your code then you will get clear error sql message

 $result = mysqli_query($con,"SELECT pnr from tbl_user where pnr = '".$pnr."'") or die(mysqli_error($con));
Sanoob
  • 2,466
  • 4
  • 30
  • 38
  • @cyclic small typo in my code, Copy paste sometimes is very bad. :p anyway you need to put a ')' this in that line before semicolon – Sanoob Mar 08 '14 at 18:02