未捕获的类型错误:mysqli_num_rows():参数#1($result)必须是类型mysqli_result,bool given in C:\wamp64\www\a\aa\index.php第40行[重复]

jutyujz0  于 2023-05-21  发布在  PHP
关注(0)|答案(1)|浏览(325)

此问题已在此处有答案

What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such(1个答案)
6小时前关闭
我一直在尝试学习更多的php为学校项目,并认为这样做的复制一些youtube教程,但我遇到了这个错误,不知道如何解决它:
index.php:

if (isset($_POST["enviar"])) {
        $Nome = $_POST["nome"];
        $Cpf = $_POST["cpf"];
        $Email = $_POST["email"];
        $Senha = $_POST["senha"];

        $SenhaHash = password_hash($Senha, PASSWORD_DEFAULT);

        $Erros = array();
        
            if (empty($Nome) OR empty($Cpf) OR empty($Email) OR empty($Senha)) {                    
            array_push($Erros,"É necessário que todos os campos sejam preenchidos!");               
            }
         
            if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {                                      
            array_push($Erros,"O e-mail não é válido.");
            }

            if (strlen($Senha)<8) {                                                                 
            array_push($Erros,"A senha deve conter pelo menos 8 caracteres");                  
            }

            require_once "database.php";

            $sql = "SELECT * FROM dados WHERE email = '$Email'";
            
            $result = mysqli_query($conn, $sql);                                                             
            $rowCount = mysqli_num_rows($result);

            if ($rowCount>0) {
            array_push($Erros,"Email já cadastrado.");
        }

        if(count($Erros)>0) {
            foreach ($Erros as $Erro) {                                                         
                echo "<div class='alert alert-danger'>$Erro";
            }
        }
        
        else{
        
            $sql = "INSERT INTO dadoscadastro (nome,cpf,email,senha) VALUES ( ?, ?, ?, ?)";
            $stmt = mysqli_stmt_init($conn); /*inicia o parametro(statement)*/
            $prepareStmt = mysqli_stmt_prepare($stmt,$sql); /*prepara o parametro(statement)*/

            if ($prepareStmt) {
            mysqli_stmt_bind_param($stmt,"siss",$Nome, $Cpf, $Email, $SenhaHash);
            mysqli_stmt_execute($stmt);
            echo "You are registered successfully.";
            }
        
            else{
            die("something went wrong.");
            }

        }

    }

index.php的html:

<form action="index.php" method="post">

    <p>Nome: <input type="text" name="nome"></p>

    <p>CPF: <input type="number" name="cpf"></p>

    <p>Email: <input type="email" name="email"></p>

    <p>Senha: <input type="password" name="senha"></p>

    <p><input type="submit" value="ENVIAR" name="enviar"></p>

</form>

database.php:

$HostName = "localhost";
$DatabaseUser = "root";
$DatabasePassword = "";
$DatabaseName = "projetopit";
$conn = mysqli_connect($HostName, $DatabaseUser, $DatabasePassword, $DatabaseName);
if (!$conn) {
    die("something went wrong;");
}

试着给另一个名字,并看到比较字母的字母与教程即时通讯以下,但即时通讯没有找到错误,如果yall可以帮助我,我会很感激,也很抱歉,如果错误是太基本,即时通讯初学者在编码和它是一个挑战,到目前为止。

tf7tbtn2

tf7tbtn21#

在调用 mysqli_num_rows() 之前,你必须测试 $result 是否是一个 mysqli result,如果在执行查询时出错,它可能是 false(参见mysqli查询手册),例如:

if ($result = mysqli_query($conn, $sql)) {

  $rowCount = mysqli_num_rows($result);
  // ... 
}

我还建议您始终使用参数化查询,而不是使用字符串连接来构建它们,即使是SELECT类型的查询,特别是如果您的参数来自用户输入。

相关问题