PHP login page using prepared statements I am new to the concept of prepared statement and i am trying to adjust my "user login" function to be a prepared statement. The part i am really struggling with is the fact i have multiple SQL queries in the one function and the biggest hurdle right now is fetching from a prepared statement.

Here is the code i have written for the function so far, i do apologise its a mess and i understand there is parts that shouldn't be in there, i just left everything in. 

function login_member() {

    $connection = mysqli_connect("localhost", "root", "", "ecom_db");
    if(isset($_POST['login_button'])) {

        $email = filter_var($_POST['log_email'], FILTER_SANITIZE_EMAIL); 

        $_SESSION['log_email'] = $email; 
        $password = $_POST['log_password']; 

        $stmt = $connection-&gt;prepare("SELECT email AND password FROM users WHERE email=? AND password=? AND com_code IS NULL");
        $stmt-&gt;bind_param('ss', $email, $password);
        $stmt-&gt;execute();
        $stmt-&gt;bind_result($email, $password);
        $stmt-&gt;store_result();
        if($stmt-&gt;execute()) {

        $result = $stmt-&gt;get_result();

        while ($row = $result-&gt;fetch_assoc()) {

            //result is in row

            }

        }
        if($stmt-&gt;num_rows == 1) {
            if($stmt-&gt;fetch()) {
                while (($row = $res-&gt;fetch_assoc()))
                   $username = $row['username'];


            $closed = $connection-&gt;prepare("SELECT email AND password FROM users WHERE email=? AND user_closed=?");
            $closed-&gt;bind_param('ss', $email, "yes");
            $closed-&gt;execute();
            $closed-&gt;bind_result($email, "yes");
            $closed-&gt;store_result();
            $closed_res = $closed-&gt;get_result();

            if($closed-&gt;num_rows == 1) {
                $open = $connection-&gt;prepare("UPDATE users SET user_closed=? WHERE email=?");
                $open-&gt;bind_param('ss', "no", $email);
                $open-&gt;execute();
                $open-&gt;bind_result("no", $email);
                $open-&gt;store_result();
            }

            $_SESSION['username'] = $username;
            header("Location: ../public/member/index.php");
            exit();
        }
        else {
            set_message("Email or password was incorrect");
        }
    }
}


I understand that it looks like a complete mess, i have left all my working outs in there. My main issue here is after my initial statement, i want to be able to fetch the results from the returned row and set the "username" a column in that row to the variable $username.

My original, working code (without prepared statements) is as follows.

My original - without prepared satatements is as follows.

function login_new_member() {

    if(isset($_POST['login_button'])) {

        $email = filter_var($_POST['log_email'], FILTER_SANITIZE_EMAIL); //sanitize email

        $_SESSION['log_email'] = $email; //Store email into session variable 
        $password = md5($_POST['log_password']); //Get password md5

        $check_database_query = query("SELECT * FROM users WHERE email='$email' AND password='$password' AND com_code IS NULL");
        confirm($check_database_query);
        $check_login_query = mysqli_num_rows($check_database_query);

        if($check_login_query == 1) {
            $row = mysqli_fetch_array($check_database_query);
            $username = $row['username'];

            $user_closed_query = query("SELECT * FROM users WHERE email='$email' AND user_closed='yes'");
            confirm($user_closed_query);
            if(mysqli_num_rows($user_closed_query) == 1) {
                $reopen_account = query("UPDATE users SET user_closed='no' WHERE email='$email'");
                confirm($reopen_account);
            }

            $_SESSION['username'] = $username;
            header("Location: ../public/member/indexhold.php?register_new");
            exit();
        }
        else {
            echo "Email or password was incorrect";
        }


    }


}
There are several problems with your code. 


An extra query to get exactly the same row you just requested 
Unreliable password hashing
Inconsistent query handling with strange query()/confirm() pair.
A lot of things intermixed here - POST, database internals, everything.


To make it better you have to store passwords after hashing them with password_hash() function and also utilize an improved version of query() function. 
So the final code would be like this (it is using my simple DB wrapper which is based on PDO, but could be implemented using mysqli as well). So the idea is that you do all the prepared statements' related stuff inside the database class, and never touch it inside a user function.

function login_new_member($email, $password)
{
    $user = DB::run("SELECT * FROM users WHERE email=? AND com_code IS NULL", [$email]);

    // you really should switch to password_hash/password_verify    
    // and make the following line as 
    // if ($user &amp;&amp; password_verify($password, $user['pass']))
    // but for now
    if ($user &amp;&amp; md5($password) === $user['password']))
    {
        if ($user['user_closed'] == 'yes')
        {
            DB::run("UPDATE users SET user_closed='no' WHERE email=?", [$email]);
        }
        $_SESSION['log_email'] = $user['email'];
        $_SESSION['username'] = $user['username'];
        header("Location: ../public/member/indexhold.php?register_new");
        exit();
    } else {
        echo "Email or password was incorrect";
    }
}


so this function should be called like this

if(isset($_POST['login_button'])) {
    login_new_member($_POST['log_email'], $_POST['log_password'])
}