繁体   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