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

时间:2014-12-05 19:10:08

标签: php mysql pdo

我开始学习PDO并在此过程中决定重写旧的mysql_*代码。所以我有一个登录表单,根据userlevel重定向到不同的位置。这样做(I think since I can login correctly)。接下来当重定向我时,我有查询,这取决于userlevel显示来自数据库的一些结果。问题是它没有返回任何内容,并且日志文件中没有错误。这是我的登录信息。我这样做了吗?

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 {

这是我根据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();
 ?>

正如我所说,我对PDO完全陌生,所以请耐心等待,如果你能帮助我的话。谢谢。

更新 - 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;
}
}
?>

3 个答案:

答案 0 :(得分:3)

您要分配给会话$id, $userlevel, $firstname, $lastname, $user_image, $email的变量在哪里? 它们在这一点上是未定义的:

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

我认为你需要的是这个

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");
}
}

答案 1 :(得分:1)

你能回复$res的内容吗? 如:

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

看看结果是什么,也许你的数组不知道$res['userlevel']的值,你的数组可能被$res[0]['userlevel']或类似的东西访问。

让我知道它是否有效

答案 2 :(得分:1)

您应该检查是否有结果集。

    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>';
   }