2014-01-11 80 views
0

我想使用PDO和$ _SESSION ['uid']能夠獲取當前成員名並將其顯示回來。使用PDO顯示當前用戶名

CREATE TABLE IF NOT EXISTS `users` (
`uid` int(10) NOT NULL AUTO_INCREMENT, 
`email` varchar(64) NOT NULL, 
`salt` varchar(16) NOT NULL, 
`password` varchar(64) NOT NULL, 
`name` varchar(255) NOT NULL, 
`birthday` date NOT NULL, 
`activation` varchar(40) DEFAULT NULL, 
PRIMARY KEY (`uid`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; 

以上是表

<?php 
require ('common.php'); // Database connection, Session Starter 
$ps = $db->prepare("SELECT * FROM `users` WHERE uid = :uid"); 
$ps->bindParam(':uid', $_SESSION['uid'];, PDO::PARAM_STR, 0); 
$ps->execute(); 
$ps->setFetchMode(PDO::FETCH_ASSOC); 
while ($ps as $row) 
{ 
echo $row['name']; 
} 
?> 

上面顯示一個空白屏幕。

<?php 
session_start(); 
Print_r ($_SESSION); 
var_dump($_SESSION); 
?> 

的print_r顯示陣列([UID] =>數組) 的var_dump顯示陣列(1){[ 「UID」] =>串(5) 「陣列」}

<?php 
require("common.php"); 

if(!empty($_POST)) 
{ 
$query = "SELECT uid, email, password, salt FROM users WHERE email = :email"; 
$query_params = array(':email' => $_POST['email']); 

try { 
$stmt = $db->prepare($query); 
$result = $stmt->execute($query_params); 
} 

catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } 

$login_ok = false; 
$row = $stmt->fetch(); 
if($row) 
{ 
$check_password = hash('sha256', $_POST['password'] . $row['salt']); 
for($round = 0; $round < 222222; $round++) 
{ 
$check_password = hash('sha256', $check_password . $row['salt']); 
} 

if($check_password === $row['password']) 
{ 
$login_ok = true; 
} 
} 

if($login_ok) 
{ 
unset($row['salt']); 
unset($row['password']); 
$_SESSION['uid'] = $row; 
header("Location: https://**"); 
die("Redirecting to: https://**"); 
} 
else 
{ 
header("Location: https://**"); 
die("Redirecting to: https://**"); 
} 
} 
?> 

包括登錄.PHP

+0

你有一個錯字你** ** bindParam: 使用這種 '$ PS-> bindParam( ':UID',$ _SESSION [ 'UID'],PDO :: PARAM_STR,0) ;' –

+0

@MohammadMasoudian試圖仍然沒有改變劇本。 –

+0

顯示$ _SESSION的內容;嘗試var_dump或print_r並更新其中的內容 –

回答

0

改變這一行

 $ps->bindParam(':uid', $_SESSION['uid'];, PDO::PARAM_STR, 0); 

 $ps->bindParam(':uid', $_SESSION['uid'];, PDO::PARAM_STR, 12); //12 means length of the data type 
+0

我試過從$ _SESSION ['uid'];中刪除了分號。仍然沒有顯示任何數據。 –

+0

是不是在我的實際代碼不知道它是如何得到這裏哈哈。 –

+0

什麼不是你的代碼 – sanjeev