2

Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?

I know this is pretty basic.. but for some reason this is not working for me. I have a form that stores a Facebook user's ID to check if they already submitted the form. I have the form that submits the User ID into the database working perfectly. Its just this part of checking if the User ID value exists in the database that is tripping me up.

Here's my code....

$user_id = 1234567890;

$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");

if ($checkUserID) {
echo "GTFO BRO";
} 

Whenever I do an "echo" on the $checkUserID variable I get this returned.. "Resource id #9"

Community
  • 1
  • 1
Dustin
  • 4,314
  • 12
  • 53
  • 91
  • 1
    Try `if (mysql_num_rows($checkUserID)>0)`! – ComFreek Nov 04 '11 at 20:05
  • Cba to answer in full, so just a comment for me. But what you are doing is running a query. The response of *any* query is always a resource. (when using mysql_*). You would then need to get the rows from that query or get the number of rows in that query. Additionally, don't use mysql_*. It's awful. Use the PDO. – Layke Nov 04 '11 at 20:07

3 Answers3

12

mysql_query returns a resource containing the result of the query, you need to use something like this:

$user_id = 1234567890;

$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");

if (!$checkUserID) {
    die('Query failed to execute for some reason');
}

if (mysql_num_rows($checkUserId) > 0) {
    echo "User id exists already.";
    $user = mysql_fetch_array($checkUserId);
    print_r($user); // the data returned from the query
}
drew010
  • 68,777
  • 11
  • 134
  • 162
0

I think you query string is wrong. If you're using double quotes, you'd have to change it to

.... WHERE fbUserId = '{$user_id}'"

or you have to concatenate it

..... WHERE fbUserId = '" . $user_id . "'"
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
dispake
  • 3,259
  • 2
  • 19
  • 22
-1

try the following piece of code:

$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");

while($test = mysql_fetch_array($checkUserID))

if ($test ) {
echo "GTFO BRO";
}

i hope this will work properly for you..

AliMohsin
  • 329
  • 1
  • 5
  • 10