[英]How to find if a field with a specific value exist in database
I am building a application in web but I want to know if is there any way to chech if in a database exist e field with a certain value. 我正在构建Web应用程序,但我想知道是否有任何方法可以检查数据库中是否存在具有特定值的e字段。 So for example I want to check if in table
Profile( Profile_Id, User_Id, Address, City...)
exist the user with User_Id=20. 因此,例如,我想检查表
Profile( Profile_Id, User_Id, Address, City...)
存在User_Id = 20的用户。 The reason that I want to check this is that if he user exist I want that when I press the button in my form to update the field of the table Profile and if the user do not exist to insert a new row in the table... 我要检查的原因是,如果他的用户存在,那么当我按下表单中的按钮以更新表Profile的字段时,以及如果该用户不存在,则希望在表中插入新行。 。
For example: 例如:
If( User_Id exist in table Profile(this is the check statement))
$update_query="update Profile...........
else
$update_query=mysql_query("Insert into Profile..............
Please hel me...Thanks in advance 请帮助我...谢谢
you are looking for 你在找
INSERT INTO Profiles (user_id,name,age) VALUES (1,"AL","33")
ON DUPLICATE KEY UPDATE name="Alan", age=34;
The SQL query for this check is: 此检查的SQL查询为:
SELECT 1
FROM Profile
WHERE User_Id = ?
You would replace the ?
您将替换
?
with the value being checked (eg 20). 检查值(例如20)。 If you use PDO , the replacement can be done using PDOStatement's bindValue method .
如果使用PDO ,则可以使用PDOStatement的bindValue方法完成替换。
If the user exists, a result set of one column will be returned. 如果用户存在,将返回一列的结果集。 If not, an empty result set will be returned.
否则,将返回空结果集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.