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"

2
  • 1
    Try if (mysql_num_rows($checkUserID)>0)!
    – ComFreek
    Commented Nov 4, 2011 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
    Commented Nov 4, 2011 at 20:07

3 Answers 3

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
}
0
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 . "'"
1
  • 1
    Concatenation into query strings is just asking for SQL injection problems. Commented Nov 13, 2013 at 13:04
-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..

Not the answer you're looking for? Browse other questions tagged or ask your own question.