This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                                5年前关闭。
            
                    
每次尝试使用此脚本创建帐户时,都会出现以下错误:“警告:mysql_num_rows()期望参数1为资源,在第16行的C:\ xampp \ htdocs \ registration.php中给出布尔值
记录已成功插入”

我正在尝试检查帐户名是否已经存在。

我已经在该网站上进行了很多搜索,但似乎无法弄清楚这个问题。

这是我的代码:

<?php
Include 'connect.php';
If(isset($_REQUEST['submit'])!='')
{
If($_REQUEST['Username']=='' || $_REQUEST['Password']=='')
{
Echo "please fill the empty field.";
}
Else
{
$result = mysql_query("SELECT Username FROM accounts WHERE Username =      ".$_REQUEST['Username']."");
$sql="insert into accounts(Username,Password) values('".$_REQUEST['Username']."',     '".$_REQUEST['Password']."')";
$res=mysql_query($sql);
if(!mysql_num_rows($result) >= 1)
{
Echo "Record successfully inserted";
}
Else
{
Echo "There is some problem in inserting record";
}
}
}
?>

最佳答案

您也必须在SQL中使用引号将字符串引起来。代替

$result = mysql_query("SELECT Username FROM accounts
WHERE Username =      ".$_REQUEST['Username']."");


采用

$result = mysql_query("SELECT Username FROM accounts
WHERE Username = '".$_REQUEST['Username']."'");


并且,请阅读有关SQL查询安全性的内容。并且请注意,mysql扩展名已过时,应使用新的mysqli扩展名或PDO代替。

关于php - mysql_num_rows期望参数1是资源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22080536/

10-16 08:21