验证记录存在的数据库条件

时间:2013-10-30 09:51:40

标签: php mysql sql database

在我的网站上 我有textbox用户输入网址。

Onece用户输入它我显示该网址的预览,其中包含该网址上的图片和小描述。

我从数据库中获取预览详细信息。

情景是这样的:

1. user enters url
2. Check whether url exist in table
3. If yes dont inset into db and fetch details like url title, desc, image
4. If not exist insert into table and then show the preview from it

那么我首先要检查它是否存在于表中?

我有$url来检查情况。

select url from post_record where url='".$url."';

我想要bool或1/0结果用于此查询。我怎么能得到它? 如果为True,那么我直接获取记录,如果结果为0,那么我首先输入详细信息,然后从表中显示预览。

这是正确的方式还是有人能想到的更好的方案。

3 个答案:

答案 0 :(得分:1)

只需选择COUNT()

即可
SELECT COUNT(*) FROM post_record WHERE url = ?

答案 1 :(得分:1)

SELECT COUNT(*)FROM post_record WHERE url = user_inputted_value

答案 2 :(得分:0)

这是对的吗?

    $result = mysqli_query("SELECT url from post_data where url='".$url."' and id='".$id."'");
        if ($url = @mysqli_fetch_array($result))
        {
            // do nothing
        }
        else
        {
            //insert url details
        }
            //After this fetch data from table to show preview
相关问题