简体   繁体   English

具有会话的PDO查询不会返回任何内容

[英]PDO query with session doesn't return anything

I'm starting to learn PDO and while doing this decided to rewrite my old mysql_* code. 我开始学习PDO ,并在此过程中决定重写我的旧mysql_*代码。 So I have a login form which according to userlevel redirects to different locations. 因此,我有一个登录表单,该表单根据用户级别重定向到不同的位置。 This is done ( I think since I can login correctly ). 这样就完成了( I think since I can login correctly )。 Next when redirects me I have query which depending of userlevel show some result from database. 接下来,当我重定向时,我有一个查询,该查询根据用户级别显示数据库的某些结果。 The problem is that it doesn't return anything and there are no errors in the logfile. 问题在于它不返回任何内容,并且日志文件中没有错误。 This is my login. 这是我的登录名。 Am I doing it correctly? 我做得对吗?

session_start();
if(isSet($_POST['submit'])) {
include 'misc/database.inc.php';
$pdo = Database::connect();

$username=$_POST['username']; 
$password=sha1($_POST['password']); 

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");

    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);

    $stmt->execute();

    $res  = $stmt -> fetch();

if ($res['userlevel'] == 1)
{
    // Save type and other information in Session for future use.
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['userlevel'] = $userlevel;

    header( "location: admins/main.php");   
}
elseif ( $res['userlevel'] >= 4 ) 
{
        $_SESSION['user_id'] = $id; 
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['userlevel'] = $userlevel;
        $_SESSION['firstname'] = $firstname;
        $_SESSION['lastname'] = $lastname;
        $_SESSION['user_image'] = $user_image;
        $_SESSION['email'] = $email;    
        header('Location: users/main.php');
}
else 
{
    header("location: index.php");
}
// Closing MySQL database connection 
$pdo = null;
} else {

And this is the query which I want to perform in main.php when login according to userlevel 这是我根据用户级别登录时要在main.php执行的查询

<?php
include '../misc/database.inc.php';
$pdo = Database::connect();
$q = "SELECT * FROM ras AS r 
    LEFT JOIN user_ras AS r2u ON r.userlevel = r2u.ras_userlevel
    LEFT JOIN users AS u ON r2u.user_userlevel = u.userlevel where menu = '".$_SESSION['userlevel']."'";

foreach($pdo->query($q) as $res)
{
    echo '<a href="users/ras.php?rest_id='. $res['ras_id'] .'">'.$res['name'].'</a>';

 }
 Database::disconnect();
 ?>

As I said I'm completely new to PDO so please bear with me and if you can help me. 就像我说的那样,我对PDO完全陌生,所以请多多包涵,如果可以的话可以帮助我。 Thank you. 谢谢。

Update - database.inc.php 更新-database.inc.php

<?php
class Database
{
private static $dbName = 'dbname' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = 'user';
private static $dbUserPassword = 'pass';

private static $cont  = null;

public function __construct() {
    die('Init function is not allowed');
}

public static function connect()
{
   // One connection through whole application
   if ( null == self::$cont )
   {     
    try
    {
      self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); 
    }
    catch(PDOException $e)
    {
      die($e->getMessage()); 
    }
   }
   return self::$cont;
}

public static function disconnect()
{
    self::$cont = null;
}
}
?>

where are the variables defined that you are assigning to session $id, $userlevel, $firstname, $lastname, $user_image, $email ? 您要分配给会话$id, $userlevel, $firstname, $lastname, $user_image, $email的变量在哪里? They are undefined at this point: 在这一点上,它们是未定义的:

    $_SESSION['user_id']   = $id; 
    $_SESSION['userlevel'] = $userlevel;
    $_SESSION['firstname'] = $firstname;
    $_SESSION['lastname']  = $lastname;
    $_SESSION['user_image']= $user_image;
    $_SESSION['email']     = $email; 

I think what you need is this 我想你需要的是

session_start();
if(isSet($_POST['submit'])) {
include 'misc/database.inc.php';
$pdo = Database::connect();

$username=$_POST['username']; 
$password=sha1($_POST['password']); 

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");

    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);

    $stmt->execute();

    $res  = $stmt -> fetch();

if ($res['userlevel'] == 1)
{
    // Save type and other information in Session for future use.
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['userlevel'] = $res['userlevel'];

    header( "location: admins/main.php");   
}
elseif ( $res['userlevel'] >= 4 ) 
{
        $_SESSION['user_id']   = $res['id'];
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;  
        $_SESSION['userlevel'] = $res['userlevel'];
        $_SESSION['firstname'] = $res['firstname'];
        $_SESSION['lastname']  = $res['lastname'];
        $_SESSION['user_image']= $res['user_image'];
        $_SESSION['email']     = $res['email']; 
        header('Location: users/main.php');
}
else 
{
    header("location: index.php");
}
}

Can you echo the contents of $res ? 您可以回显$res的内容吗? such as: 如:

echo "<pre>";
print_r($res);
echo "<pre>";

and see what the result is, maybe your array doesn't know the value of $res['userlevel'] , your array might be accessed as $res[0]['userlevel'] or something like that. 并查看结果是什么,也许您的数组不知道$res['userlevel'] ,您的数组可能以$res[0]['userlevel']或类似的名称进行访问。

Let me know if it works 让我知道是否有效

You should check to see if you have a result set. 您应该检查是否有结果集。

    if ($res) {

    foreach($pdo->query($q) as $res)
    {
        echo '<a href="users/ras.php?rest_id='. $res['ras_id'] .'">'.$res['name'].'</a>';

     }
    } else {
    echo '<p>no result</p>';
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM