簡體   English   中英

未定義的索引:會話循環不工作 php、pdo oop

[英]Undefined index:session LOOP NOT WORKING php, pdo oop

我使用 PHP - OOP - PDO 制作用戶在線頁面

include_once '../database.php'; $db = 新數據庫();

$getRows = $db->getRows('select * from visitors_online');
$gr = $db->rowCount();
$online = '';


$getRow = $db->getRow('select * from user_online');
$gr2 = $db->rowCount();

if(!empty($gr2)) {
        try {
            while ($getR = $getRow){
            $getRow = $db->getRow('select * from users where id = ?',[$getR['session']]);
            echo  ', &nbsp <a href="dashboard.php?user='.$getRow['username'].'">'.$getRow['username'].'</a> &nbsp  ';
            }
        } catch (PDOException $e) {
            die('Error :'.  $e->getMessage());
        }
    $total = $gr + $gr2;

問題是: * 不顯示除管理員以外的任何用戶,我也得到了這個:

ONLINE 

行政
注意:未定義索引:第 56 行 /Applications/MAMP/htdocs/baws/admin/online.php 中的會話,
.Users = 0 ,Member = 2 , Register = 2

誰是在線名單

這是來自 Database 類的函數

// Get row by id, username, or email etc..
    public function getRow($query, $para = []){
        try {
            $this->stmt = $this->datab->prepare($query);
            $this->stmt->execute($para);
            return $this->stmt->fetch();
        } catch (PDOException $e) {
            throw new Exception($e->getMessage());
        }
    }

任何幫助

謝謝

我試圖簡化你的代碼,因為我不知道你的課程細節,而且很亂。

問題是你沒有正確綁定東西,也沒有正確獲取它們。 此外,您正在准備第二個查詢,每次在查詢 1 results 中循環時,這是無用的。 准備兩個(無論是否與您的課程),然后綁定並執行。

$stmt1 = $db->prepare('select * from user_online where id= ?');
$result1 = getRows($stmt1, "1");
$gr1 = $db->rowCount();

if (!empty($gr1)) {

    $stmt2 = $db->prepare('select * from users where id = ?');

    foreach ($result1 as $key1 => $h1) {
        $stmt2->bindParam(1, $h1['session'], PDO::PARAM_INT);
        $stmt2->execute();
        $result2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        if (count($result2) !== 0) {
            foreach ($result2 as $key2 => $r2) {
                echo ', &nbsp <a href="dashboard.php?user=' . $r2['username'] . '">' . $r2['username'] . '</a> &nbsp  ';
            }
        }
    }
}

function getRow($query, $para) {
    $stmt1->bindParam(1, $para, PDO::PARAM_INT);
    try {
        $stmt1->execute($para);
        $result1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
        return $result1;
    } catch (PDOException $e) {
        throw new Exception($e->getMessage());
    }
}

請在這里找到數據庫類

class database {
    public $isConn;
    protected $datab;
    private $stmt;
public function __construct() {
    $this->connect();
}

    // connect to database
    private function connect(){
        $host   = 'localhost';
        $db     = 'baws';
        $user   = 'root';
        $pass   = 'root';
        $option = [];

        $this->isConn = TRUE;
        try {
            $this->datab = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pass, $option);
            $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            echo '<h3>Not connected</h3>' . $e->getMessage();
        }

    }

    // Disconnected from database
    private function disconnect(){
        $this->isConn = NULL;
        $this->datab = FALSE;
    }

    //insert to database
    public function insertRow($query, $para = []){
        try {
            $this->stmt = $this->datab->prepare($query);
            $this->stmt->execute($para);
            return TRUE;
        } catch (PDOException $e) {
            throw new Exception($e->getMessage());
        }
    }

    //update row to database
    public function updateRow($query, $para = []){
        $this->insertRow($query, $para);
    }

    //Delete row from database
    public function deleteRow($query, $para = []){
        $this->insertRow($query, $para);
    }

    // Get row by id, username, or email etc..
    public function getRow($query, $para = []){
        try {
            $this->stmt = $this->datab->prepare($query);
            $this->stmt->execute($para);
            return $this->stmt->fetch();
        } catch (PDOException $e) {
            throw new Exception($e->getMessage());
        }
    }

}

online.php 頁面

ob_start();
echo "ONLINE <br>";
include_once '../database.php';
$db = new database();

try {
    $session=$_COOKIE['id'];
    $time=time();
    $time_check=$time-300; //SET TIME 10 Minute

    $getRow = $db->getRow("SELECT * FROM user_online WHERE session = ?", [$session]);
    $count =$db->rowCount($getRow);


    if($count == '0'){
        $insertRow = $db->insertRow("INSERT INTO user_online(session, time)VALUES(? , ?)",[$session, $time ]);
    }

    elseif($count != '0'){
        $updateRow = $db->updateRow("UPDATE user_online SET time = ? WHERE session = ?", [$time, $session]);
    }else{
        $deleteRow = $db->deleteRow("DELETE FROM user_online WHERE time < ? ", [$time_check]);
    }
} catch (PDOException $e) {
   die('Error :'.   $e->getMessage());
}


try {
    $ip=$_SERVER['REMOTE_ADDR'];
    $session=$ip;
    $time=time();
    $time_check=$time-300; //SET TIME 10 Minute

    $deleteRow = $db->deleteRow("DELETE FROM visitors_online WHERE time < ? ", [$time_check]);


} catch (PDOException $e) {
    throw new Exception($e->getMessage());

}

$getRows = $db->getRows('select * from visitors_online');
$gr = $db->rowCount();
$online = '';


$getRow = $db->getRow('select * from user_online');
$gr2 = $db->rowCount();

if(!empty($gr2)) {
        try {
            while ($getR = $getRow){
            $getRow = $db->getRow('select * from users where id = ?',[$getR['session']]);
            echo  ', &nbsp <a href="dashboard.php?user='.$getRow['username'].'">'.$getRow['username'].'</a> &nbsp  ';
            }
        } catch (PDOException $e) {
            die('Error :'.  $e->getMessage());
        }
    $total = $gr + $gr2;

} //結尾

暫無
暫無

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

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