警告:mysqli_fetch_array()要求参数1为mysqli_result,boolean

时间:2015-01-14 12:42:19

标签: mysql passwords boolean

尝试创建更改密码'我的网站页面,我一直面对这两个错误,我似乎无法理解它们出现的原因;

  

警告:mysqli_fetch_array()要求参数1为mysqli_result,第51行/home/ll12rth/public_html/COMM2735/database/password.php中给出布尔值   警告:mysqli_free_result()要求参数1为mysqli_result,布尔值在第139行的/home/ll12rth/public_html/COMM2735/database/password.php中给出

<?php
session_start();
$db_hostname = 'localhost';

    $db_database = "****"; //replace with your db name
    $db_username = "****"; //replace with the db username that you created
    $db_password = "****"; //replace with the db password that you created
    $db_status   = 'not initialised';   
    $db_server   = mysqli_connect($db_hostname, $db_username, $db_password);
    $db_status   = "connected";

    if (!$db_server) {
        die("Unable to connect to MySQL: " . mysqli_connect_error());
        $db_status = "not connected";
    } else 

require_once('checklog.php');
require_once("functions.php");



// Grab the form data

$username = trim($_POST['username']);
$password = trim($_POST['password']);
$newpassword = trim($_POST['newpassword']);
$repeatpassword = trim($_POST['repeatpassword']);

if (isset($_POST['submit'])) {
    if ($username && $password) {
        $username = clean_string($db_server, $username);
        $password = clean_string($db_server, $password);
        $query = "SELECT * FROM users WHERE username='$username'";

        $result = mysqli_query($db_server, $query);

        if ($row = mysqli_fetch_array($result)) {
            $db_username = $row['username'];
            $db_password = $row['password'];

            if ($username == $db_username && salt($password) == $db_password) {
                $_SESSION['username'] = $username;
                $_SESSION['logged'] = "logged";

                // header('Location: home.php');
                // PASSWORD CHANGING IS DONE HERE
                if ($newpassword == $repeatpassword) {
                    //From register
                    if (strlen($newpassword) > 25 || strlen($newpassword) < 6) {
                        $message = "Password must be 6-25 characters long";
                    } else {
                        //part 8
                        // Process details here

                        //include file to do db connect

                        if ($db_server) {
                            //clean the input now that we have a db connection

                            $newpassword    = clean_string($db_server, $newpassword);
                            $repeatpassword = clean_string($db_server, $repeatpassword);
                            mysqli_select_db($db_server, $db_database);

                            // check whether username exists

                            $query = "SELECT password FROM users WHERE password='$newpassword'";

                           $result=mysqli_query($db_server, $query);

                            if ($row = mysqli_fetch_array($result)){
                                $message = "This is your current password. Please try again.";
                            } else {
                                //part 9
                                // Process further here
                                $newpassword = salt($newpassword);

                                $query = "INSERT INTO users (password) VALUES 

                                        ('$password')";

                                mysqli_query($db_server, $query) or die("Insert failed. " . mysqli_error($db_server));
                                $message = "<h1>Your password has been changed!</h1>";
                            }

                            mysqli_free_result($result);

                        } else {

                            $message = "Error: could not connect to the database.";

                        }

                        require_once("php/db_close.php"); //include file to do db close
                    }
                }
                //This code appears if passwords dont match 
                else {
                    $message = "<h1>Your new passwords do not match! Try again.</h1>";
                }

            } else {
                $message = "<h1>Incorrect password!</h1>";
            }
        } else {
            $message = "<h1>That user does not exist!</h1>" . "Please <a href='password.php'>try again</a>";
        }
        mysqli_free_result($result);

      //Close connection!
mysqli_close($db_server);

    } else {

        $message = "<h1>Please enter a valid username/password</h1>";

    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Techothing password</title>

<div class="aboutcontainer">    
<h1>What do you want to change your password to <?php
echo $_SESSION['username'];
?>?</h1>

<form action='password.php' method='POST'>

Current Username: <input type='text' name='username'><br />

Current Password: <input type='password' name='password'><br />

New Password: <input type='password' name='newpassword'><br />

Repeat New Password: <input type='password' name='repeatpassword'><br />

<input type='submit' name='submit' value='Confirm'>

<input name='reset' type='reset' value='Reset'> 

</form>

<?php
echo $message
?>
<br  />

</div>
</div>

</div>

</div>    

</body>
</html>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这一行

        $result = mysqli_query($db_server, $query);

返回结果对象,或者,如果查询因某种原因失败,则返回false

开发此类代码的大多数人,尤其是当我们不熟悉它时,会检查这些错误。你可以大致这样做。

       if ( false === $result ) {
               printf("Errormessage: %s\n", msqli_error($db_server));
       }