//authentication process starts
$sel = "SELECT * FROM g_admin WHERE loginname='$ln' AND apassword='$pw'";
$res = executeQuery($sel);
echo "kiran ".$res;
if(totalRecords($res) == 0){
echo "Incorrect LoginName/password";
}else{
$rec = getAssoc($res);
$_SESSION['AdminId'] = $rec['Id'];
header("Location: Home.php");
}
//authentication process ends
}
function executeQuery($qry){
return mysql_query($qry) or die ('MySQL Not found // Could Not Connectquery.');
}
function totalRecords($res){
return mysql_num_rows($res) or die ('MySQL Not found // Could Not Connectres.');
}
function getAssoc($res){
return mysql_fetch_assoc($res) or die ('MySQL Not found // Could Not Connectres.');
}
In the above code $res returns 1. But totalRecords($res) thows mysql_num_rows() expects parameter 1 to be resource, boolean given in c:/db.php
新添加的函数位于数据库中.php通过使用这些函数,我们正在执行代码。executeQuery($qry) 工作正常,返回结果为 1。接下来我们调用 totalRecords($res),在这种情况下,它会抛出警告。
可能的错误:
$sel = "SELECT * FROM g_admin WHERE loginname=`'$ln'` AND apassword=`'$pw'`";
在查询中使用变量时,它应该是这样的...
$sel = "SELECT * FROM g_admin WHERE loginname='{$ln}' AND apassword='{$pw}'";
或
$sel = "SELECT * FROM g_admin WHERE loginname='".$ln."' AND apassword='".$pw."'";
顺便一提
您是否正在使用MySql 5.5?因为mysql_num_rows
扩展已被弃用。 最好在查询中使用count(*)
或使用PDO。