【问题标题】:Undefined index:session LOOP NOT WORKING php, pdo oop未定义的索引:会话循环不工作 php,pdo oop
【发布时间】:2016-08-26 14:55:21
【问题描述】:

我使用 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

谁在线列表

这是来自数据库类的函数

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

任何帮助

谢谢

【问题讨论】:

  • 您将 $para 作为空数组传递。不会工作。或者简单地执行();
  • 我从类中传递了我需要的东西 $getRow = $db->getRow('select * from users where id = ?',[$getR['session']]);。 .... 所以你怎么看?如何解决它
  • 我敢打赌这不是你的完整代码。您发布的内容缺少大括号。
  • 这是我第二次在这里添加帖子,我在这里添加代码时遇到问题我不知道那是不是来自我的笔记本电脑

标签: php mysql oop pdo


【解决方案1】:

我试图简化你的代码,因为我不知道你的课程细节而且很乱。

问题是您没有正确绑定东西,也没有正确获取它们。此外,您正在准备第二个查询,每次您在查询 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());
    }
}

【讨论】:

    【解决方案2】:

    请在此处找到数据库类

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

    }

    【讨论】:

      【解决方案3】:

      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;
      

      } //结束

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-06
        • 2017-02-02
        • 2013-05-22
        • 2016-06-07
        • 1970-01-01
        相关资源
        最近更新 更多