如何检查值是否已经存在于MySQL数据库中 [英] How to check if value already exists in MySQL database

查看:741
本文介绍了如何检查值是否已经存在于MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

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

我知道这是相当基本的..但由于某种原因,这不是为我工作。我有一个表单,存储Facebook用户的ID,以检查他们是否已提交表单。我有一个形式,提交用户ID到数据库工作完美。

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";
} 

每当我对$ checkUserID变量做一个echo ..Resource id#9

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

推荐答案

mysql_query返回包含查询结果的资源,需要使用类似这:

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
}

这篇关于如何检查值是否已经存在于MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆