检查数据库中是否存在数据 [英] Checking if data exists in database
本文介绍了检查数据库中是否存在数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查所需数据是否在数据库中的正确方法是什么?
What is the right way of checking if the required data is in the database?
我目前使用的是
mysql_query("SELECT anyfield FROM table WHERE field='$data'");
,然后检查是否有任何行受到影响.
and then check the if any rows are affected.
但是我对提取的数据anyfield
确实没有任何用处.
尽管这里的资源使用情况很小,但是在不从表中提取任何其他字段的情况下,检查数据库中是否存在数据的正确方法是什么?
But I dont really have any use with the extracted data anyfield
.
Eventhough the resource usage is so minor here, what is the right way to check if data exists in a db without extracting any other fields from the table?
推荐答案
让数据库计数并从查询中检索计数数据.
Let the database count and retrieve the count data from the query.
$result = mysql_query('SELECT COUNT(*) FROM `table` WHERE `field` = ...');
if (!$result) {
die(mysql_error());
}
if (mysql_result($result, 0, 0) > 0) {
// some data matched
} else {
// no data matched
}
这篇关于检查数据库中是否存在数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文