【发布时间】:2015-05-06 18:28:05
【问题描述】:
我一直在努力,但我无法让它发挥作用。 我有问题:
1.警告:第 22 行 C:\wamp\www\pdo\session\session.php 中的非法字符串偏移“用户名”
2.变量$login_session只存储第一个字母。
代码如下:
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$hostname='localhost';
$username='root';
$password='';
//test PDO
try {
$dbh = new PDO("mysql:host=$hostname;dbname=company",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$sql="select username from login where username='$user_check'";
$result = $dbh->prepare($sql);
$result->execute();
$row = $result->fetchColumn();
$login_session =$row['username'];
if(!isset($login_session)){
$dbh = null; // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
非常感谢您的宝贵时间!
【问题讨论】: