如何使用预准备语句将特定行存储在会话中?

时间:2017-03-13 08:18:40

标签: php sql session row

如何使用预准备语句将特定行存储到会话中?我在使用它时遇到了问题。

这是我的代码。

PHP代码

<?php
include "connection.php";
session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if($username == $_POST['username'] && $password == $_POST['password'])
{
  // prepare an sql statement
  $stmt = $conn->prepare("select username, 
                              firstname, 
                              lastname, 
                              email 
                          from users 
                          where username = ? and password = ?");
  //bind the sql statement
  $stmt->bind_param("ss", $myusername, $mypassword);

  // set parameters, and to protect sql injection
  $myusername = stripcslashes($username);
  $password = stripcslashes($password);

  $myusername = mysqli_real_escape_string($conn,$username);
  $mypassword = mysqli_real_escape_string($conn,$password);

  $_SESSION['user'] = $myusername;
  $_SESSION['password'] = $mypassword;

  $result = $stmt->get_result();

  $output = array();
  while ($row = $result->fetch_assoc())
  {
      $_SESSION['firstname'] = $row['firstname'];
  }

  header('Location: /laurence/FirstDayTraining/Model/users.php');


}
else {
  echo "Not an admin";
}
$stmt->close();
$conn->close();

?>

我尝试过使用它:

  $result = $stmt->get_result();

  $output = array();
  while ($row = $result->fetch_assoc())
  {
      $_SESSION['firstname'] = $row['firstname'];
  }

但我得到的结果是致命的错误

Fatal error: 
    Call to a member function fetch_assoc() on a non-object in loginbackend.php on line 28

似乎有什么问题?请帮忙

0 个答案:

没有答案