警告:mysqli_fetch_assoc()期望恰好有1个参数,给定2个

时间:2018-06-22 05:44:30

标签: php html mysqli error-handling syntax-error

我遇到了一个小问题...我花了一些时间尝试创建mysqli登录脚本,但是除了遇到问题外没有做任何事情...我尝试了很多事情,但是每次都出错。现在,我来到了一个无法再独自解决问题的时代。我得到的错误是“警告:mysqli_fetch_assoc()期望参数1为mysqli_result,对象在D中给出:-FTP- WEB- WEB 5.0 \ index.php,在第26行“ -我的代码是:

<?php  
   $connect = mysqli_connect("localhost", "root", "", "testing");  
   session_start();  
   if(isset($_SESSION["username"]))  
   {  
       header("location:entry.php");  
   }  

   if(isset($_POST["login"]))  
   {  
       if($_POST["username"] == '' || $_POST["pin"] == '')  
       {  
            echo '<script>alert("Alle felter SKAL udfyldes!")</script>';  
       }  
       else  
       {  
           $username = mysqli_real_escape_string($connect, $_POST["username"]);  
           $password = mysqli_real_escape_string($connect, $_POST["password"]);  
           $pin = mysqli_real_escape_string($connect, $_POST["pin"]);  
           $password = sha1($password);  
           $pin = sha1($pin);
           $query = "SELECT * FROM users WHERE username = '$username'";  
           $result = mysqli_query($connect, $query);  
           if(mysqli_num_rows($result) > 0)  
           {
                $row = mysqli_fetch_assoc($connect, $result) ;
                $pass = $row['password'] ;
                $check_pin = $row['pin'] ;

                if ($password === $pass && $pin === $check_pin){
                   $_SESSION['username'] = $username;  
                   header("location:entry.php");
                }    
           }  
           else  
           {  
                echo '<script>alert("Forkert brugernavn, adgangskode eller pin-kode")</script>';  
           }  
      }  
 }  
 ?>
 <!DOCTYPE html>  
 <html>  
      <head>  
	  <script>
	  var loc = window.location.href+'';
if (loc.indexOf('http://')==0){
    window.location.href = loc.replace('http://','https://');
}
	  </script>
           <title>
		  MJVS - Private area.
		   </title>  
		   <link rel="shortcut icon" href="/title_logo.png" />
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="style.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

	  </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:500px;">  
                <h3 align="center"></h3>  
                <br /> 
                <h3 align="center"><b>Login</b></h3>  
                <br />  
                <form method="post">  
                     <label>Enter Username</label>  
                     <input type="text" name="username" class="form-control" />  
                     <br />  
                     <label>Enter Password</label>  
                     <input type="password" name="password" class="form-control" />  
                     <br />  
					 <label>Enter PIN</label>  
                     <input type="password" name="pin" class="form-control" maxlength="4" />  
                     <br />  
                     <input type="submit" name="login" value="Login" class="btn btn-info" />  
                     <br />   
                </form>  
                <?php       
                
?>
           </div>  
      </body>  
 </html>

我希望有人能帮助我解决我的问题...

3 个答案:

答案 0 :(得分:1)

看看documentation。该函数采用一个参数。特别是在您的情况下,$result参数是

  

仅用于程序样式:mysqli_query(),mysqli_store_result()或mysqli_use_result()返回的结果集标识符

您给它两个。只传递结果变量。

简而言之,您有:

$result = mysqli_query($link, $query)

获得结果集的地方(其中$link代表连接),

$row = mysqli_fetch_assoc($result)

以获得通用行。该文档本身有两个示例。

答案 1 :(得分:0)

mysqli_fetch_assoc函数仅需要一个参数(mysqli_query函数返回的结果集)。

替换

mysqli_fetch_assoc($connect, $result);

使用

mysqli_fetch_assoc($result);

并详细了解Spree Official Installation Guide

答案 2 :(得分:0)

可以在“ mysqli_real_escape_string”中使用一个参数,让我知道是否有效吗?