PHP不检查MySQL数据库中是否已有记录

时间:2014-11-08 10:37:25

标签: php mysql

我有一个问题,所以我想这样做,如果用户偶然搜索并且其代码不再被激活。通过这种方式,我做了一个简单的代码:

$loginu = $_SESSION['login'];
$query = "SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode = $numer AND usrlogin = $loginu ";
$result2 = mysql_query($query);
echo mysql_error();
if (mysql_num_rows($result2) == 0) {
    not found
}
else {
    found
}

并且崩溃了以下错误

  

未知专栏' Kamil'在' where子句'   警告:mysql_num_rows()期望参数1为资源,第56行的C:\ xampp \ htdocs \ kod.php中给出布尔值   在这种情况下,我犯了一个错误?

请帮忙,

迎接。

3 个答案:

答案 0 :(得分:2)

如果这些字符串不是整数,请确保这些字符串用引号括起来:

WHERE idcode = $numer AND usrlogin = '$loginu'

并确保确实已定义此变量$numer,因为在您的问题中没有定义它。

强制性说明:

  

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

以下是在mysqli中使用预处理语句时的样子:

$loginu = $_SESSION['login'];
$numer = // make sure this is defined!    

$db = new mysqli('localhost', 'username', 'password', 'database_name');

$query = "SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode = ? AND usrlogin = ?";
$select = $db->prepare($query);
// binding them
$select->bind_param('is', $numer, $loginu);
$select->execute();

if($select->num_rows > 0) {
    // found
} else {
    // not found
}

答案 1 :(得分:0)

    check this code and if condtion check to not blank a session data.
    and check a query value is yes to found and else to not found print.

    $loginu = $_SESSION['login'];
    if(isset($loginu))
    {
        $query="SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode='".$numer."' AND usrlogin ='".$loginu."'";
        $result = mysql_query($query);
        if(mysql_num_rows($result) > 0) 
        {
            echo "found";
        }
        else
        {
            echo "not found";
        }

    }

答案 2 :(得分:0)

尝试在查询中更改此行 在哪里idcode ='"。$ numer。"' AND usrlogin ='"。$ loginu。"'