【问题标题】:PDO PHP MYSQL SESSIONPDO PHP MYSQL 会话
【发布时间】: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();
    }


?>

非常感谢您的宝贵时间!

【问题讨论】:

    标签: php mysql session pdo


    【解决方案1】:

    而不是下面的代码:

    $login_session =$row['username'];
    

    使用这个:

    $login_session =$row;
    

    fetchColumn 中,您需要指定要获取的行。默认情况下,它从结果集中的下一行获取第一列。

    Reference.

    【讨论】:

    • @SergioHV 很高兴为您提供帮助。
    • 现在我发现了一个问题。如果我使用你所说的,会话没有用,你可以输入 URL 而无需登录,只需键入链接。我找到了一个不同的解决方案:使用 $login_session =$user_check。这个对吗?似乎工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2012-12-08
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多