簡體   English   中英

PDO PHP MYSQL會話

[英]PDO PHP MYSQL SESSION

我一直在努力,但無法使其正常運轉。 我要發出的問題:

1.警告:第22行的C:\\ wamp \\ www \\ pdo \\ session \\ session.php中的字符串偏移量'username'不合法

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();
    }


?>

非常感謝您的寶貴時間!

而不是下面的代碼:

$login_session =$row['username'];

使用這個:

$login_session =$row;

fetchColumn您需要指定要獲取的行。 默認情況下,它從結果集中的下一行獲取第一列。

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM