检查我的数据库表中是否存在值

时间:2015-10-03 12:05:31

标签: php mysql database

我想检查我的MySQL数据库表中是否存在URL,例如Url=existmessage=url是否已存在

<?php
if(isset($_POST['Submit'])){
    $dbhost = 'localhost';
    $dbuser = '####';
    $dbpass = '#######';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $Title = $_POST['Title'];
    $Url = $_POST['Url'];
    $email_data = $_POST['email_data'];
    $type_data = $_POST['type_data'];
    $sql = "INSERT INTO table ". "(Title,Url,email_data,type_data)"."VALUES('$Title','$Url','$email_data','$type_data')";
    mysql_select_db('dbname');
    $retval = mysql_query( $sql, $conn );
        if(! $retval ){
            die('Could not enter data: ' . mysql_error());
        }
    echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
    mysql_close($conn);
} else {
?>

3 个答案:

答案 0 :(得分:1)

你可以这样做

$sql = "SELECT Url FROM 'your_table_name' WHERE Url = $_POST['Url']";

运行此sql,如果返回true,则可以说它存在。

答案 1 :(得分:0)

通过查看代码有点难以知道你在做什么,但如果你想知道如何检查数据库中是否有东西你可以这样做:

<强> PHP:

$sql = mysql_query("SELECT * FROM dbname WHERE Url='$Url'");
if(mysql_num_rows($sql) > 0){
    echo "alreday exist";
}

答案 2 :(得分:0)

$query = mysql_query('select url from table-name where url=$_post['url']');
if(mysql_fetch_rows($query) != 0){
echo "URL allready Exists";
}else{ 
Insert Query 
}

嗨,试试这段代码。