php - PDO query with session doesn't return anything -
i'm starting learn pdo
, while doing decided rewrite old mysql_*
code. have login form according userlevel redirects different locations. done (i think since can login correctly
). next when redirects me have query depending of userlevel show result database. problem doesn't return , there no errors in logfile. login. doing 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 * users username = :username , password = :password"); $stmt->bindparam(':username', $username); $stmt->bindparam(':password', $password); $stmt->execute(); $res = $stmt -> fetch(); if ($res['userlevel'] == 1) { // save type , other information in session 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 query want perform in main.php
when login according userlevel
<?php include '../misc/database.inc.php'; $pdo = database::connect(); $q = "select * ras r left join user_ras r2u on r.userlevel = r2u.ras_userlevel left join users u on r2u.user_userlevel = u.userlevel menu = '".$_session['userlevel']."'"; foreach($pdo->query($q) $res) { echo '<a href="users/ras.php?rest_id='. $res['ras_id'] .'">'.$res['name'].'</a>'; } database::disconnect(); ?>
as said i'm new pdo please bear me , if can me. thank you.
update - 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 not allowed'); } public static function connect() { // 1 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 variables defined assigning session $id, $userlevel, $firstname, $lastname, $user_image, $email
? undefined @ point:
$_session['user_id'] = $id; $_session['userlevel'] = $userlevel; $_session['firstname'] = $firstname; $_session['lastname'] = $lastname; $_session['user_image']= $user_image; $_session['email'] = $email;
i think need 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 * users username = :username , password = :password"); $stmt->bindparam(':username', $username); $stmt->bindparam(':password', $password); $stmt->execute(); $res = $stmt -> fetch(); if ($res['userlevel'] == 1) { // save type , other information in session 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"); } }
Comments
Post a Comment