查询两个SQL条目?

时间:2011-08-14 02:10:56

标签: php mysql sql

我有一个SQL数据库,我正在尝试用PHP查询。这是用户帐户,我需要测试数据库中是否存在某个用户名和密码组合。

目前,我可以使用以下方法检查是否存在用户名:

 <?php
session_start();

include "config.php";

if($_POST['nameQuery']) {

$query = "SELECT * FROM ichatlogin WHERE name = '" .$_POST['nameQuery']. "'";  
$result = mysql_query($query);  
    if (mysql_num_rows($result) > 0) { 
        //User exists
        echo '1'; 
    } else { 
        mysql_query($query);
    //User does not exist
    echo '0'; 
    }
}
?>

谢谢!

2 个答案:

答案 0 :(得分:4)

您可以根据需要进行尽可能多的逻辑比较。根据需要将它们与AND或OR组合。

$name = mysql_real_escape_string($_POST['nameQuery']);
$password = mysql_real_escape_string($_POST['password']);

$query = "SELECT * FROM ichatlogin WHERE name = '$name' AND password = '$password' LIMIT 1";

答案 1 :(得分:0)

 <?php
session_start();

include "config.php";

if($_POST['nameQuery'] && $_POST['passwordQuery']) {

$query = "SELECT * FROM ichatlogin WHERE name = '" .$_POST['nameQuery'] . ' AND WHERE password = ' . $_POST['passwordQuery'];  
$result = mysql_query($query);  
    if (mysql_num_rows($result) > 0) { 
        //User exists
        echo '1'; 
    } else { 
        mysql_query($query);
    //User does not exist
    echo '0'; 
    }
}
?>