如何检查值是否已经存在于MySQL数据库中

编程入门 行业动态 更新时间:2024-10-12 10:21:50
本文介绍了如何检查值是否已经存在于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数据库中

本文发布于:2023-11-28 18:29:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!