49

I'm have some trouble checking if a Facebook User_id already exists in my database (if it doesn't it should then accept the user as a new one and else just load the canvas application). I ran it on my hosting server and there was no problem, but on my localhost it gives me the following error:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

Here's my code:

<?
$fb_id = $user_profile['id'];
$locale = $user_profile['locale'];

if ($locale == "nl_NL") {
    // Checking User Data @ WT-Database
    $check1_task = "SELECT * FROM `users` WHERE `fb_id` = " . $fb_id . " LIMIT 0, 30 ";
    $check1_res = mysqli_query($con, $check1_task);
    $checken2 = mysqli_fetch_array($check1_res);
    print $checken2;
    // If the user does not exist @ WT-Database -> insert
    if (!($checken2)) {
        $add = "INSERT INTO users (fb_id, full_name, first_name, last_name, email) VALUES ('$fb_id', '$full_name', '$first_name', '$last_name', '$email')";
        mysqli_query($con, $add);
    }
    // Double-check, the user won't be able to load the app on failure inserting to the database
    if (!($checken2)) {
        echo "Excuse us " . $first_name . ". Something went terribly wrong! Please try again later!";
        exit;
    }
} else {
    include ('sorrylocale.html');
    exit;
}

I've read it has something to do with my query being wrong, but it has worked on my hosting provider so that can't be it!

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mats de Swart
  • 538
  • 1
  • 4
  • 10
  • 5
    You need to add error handling for your queries so you can find out exactly why it is failing. You also have a significant SQL injection vulnerability. – Mike Brant Mar 15 '13 at 18:45
  • 5
    Please, **please** use the `mysqli` prepared statement feature to [properly escape your SQL queries](http://bobby-tables.com/php). – tadman Mar 15 '13 at 18:59

1 Answers1

128

The query given to mysqli_query() is failing and returning false.

Put this after mysqli_query() to see what's going on.

if (!$check1_res) {
    trigger_error(mysqli_error($con), E_USER_ERROR);
}

For more information:

http://www.php.net/manual/en/mysqli.error.php

castis
  • 8,154
  • 4
  • 41
  • 63