我想使用PDO和$ _SESSION ['uid']来获取当前成员的名称并将其显示回来。

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显示Array([uid] => Array)
Var_dump显示array(1){[“ uid”] => string(5)“ Array”}

<?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://**");
}
}
?>


包含login.php

最佳答案

更改此行

       $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

关于php - 使用PDO显示当前用户名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21058383/

10-11 03:31
查看更多