2014-10-29 4 views
0

나는 PHP에서 예전 방식으로 코드를 작성하고이 포럼을 통해 POO에 소개되었습니다. mysql에있는 스크립트를 PDO로 다시 작성합니다. 이 스크립트는 웹 사이트에 연결된 회원의 번호와 이름을 보여줍니다. ?연결된 사용자의 수와 이름 표시

<?php 

session_start(); 

if(isset($_SESSION['nom'])){ 
    include('class.connect.php'); 
    include('class.user.php'); 

    $db = new DBEngine(); 

    //verify if the name is already in the table 
    $log = $db->con->prepare('SELECT COUNT(nom) AS name FROM members_connected WHERE nom=?'); 
    $log->execute(array($_SESSION['nom'])); 


    $count = $log->fetchColumn(0); 
    $time = time(); 
    if ($count == 0) 
    { 

     // the user is not in the new table, i add him 
     $log = $db->con->prepare('INSERT INTO members_connected (nom,timestamp) VALUES(?,?)'); 
     $log->execute(array($_SESSION['nom'],$time)); 
    } 

    //name already in the table, update the timestamp 
    else 
    { 

     $log = $db->con->prepare('UPDATE members_connected SET timestamp=? WHERE nom=?'); 

     $log->execute(array($_SESSION['name'],$time)); 
    } 

    //5 min earlier's timestamp 
    $timestamp_5min = time() - (60 * 5); 

    $log = $db->con->prepare('DELETE FROM members_connected WHERE timestamp < ?'); 
    $log-> execute(array($timestamp_5min)); 

    $log = $db->con->prepare('SELECT nom FROM members_connected'); 
    $log->execute(); 
    $row = $log->fetch(PDO::FETCH_ASSOC); 

    echo $count ; 

    //show the list of connected    
    if($count > 0) 
    { 
     $i=0; 
     while($count = $log->fetch(PDO::FETCH_ASSOC)); 
     { 
      $i++; 
      echo $count['nom']; 
      if($i<$row) 
      { 
       //space between names 
       echo ','; 
      } 
     } 
    } 
} 

>

    ` 

어떤 생각하십시오 : 지금까지 그것은 단지 스크립트를 업데이트하는 수 있지만 아직

연결된 회원

의 이름의 목록이 표시?

+2

POO? 글쎄, 그것은 나를 껄끄럽게 만들었다. ;-) – Strawberry

+0

글쎄, 무엇이 오류입니까? – Eliel

+0

@ 엘리엘 그것입니다. 치명적인 오류 : C : \ Program Files (x86) \ object \ context \ bladuo \ html \ connected_members.php에있는 객체 컨텍스트가 아닐 때 $ this를 사용하십시오. – Kuma

답변

1

만들기 (당신이 BTW 첫 번째 쿼리에 무슨이다) 그래서 같은 execute에 직접 전달할 수 다음과 같이 데이터베이스를 가져오고 업데이트하는 TimeStamp 클래스 :

class TimeStamp 
    { 
     protected $db; 

     public $row_count; 
     public function __construct($db) 
      { 
       $this->db = $db; 
      } 

     // This should grab all users connected 
     public function Fetch($_user, $interval = '1') 
      { 
       // This is just checking a time range and collecting names 
       // You may want to make a new function that will then take the return list and query your user info table to get the user info 
       $connected = $this->db->con->prepare("select * FROM members_connected WHERE timestamp > DATE_SUB(NOW(), INTERVAL $interval MINUTE)"); 
       $connected->execute(array($_user)); 

       if($connected->rowCount() > 0) { 
         while($result = $connected->fetch(PDO::FETCH_ASSOC)) { 
           $users[] = $result; 
          } 

        } 

       // This should get the count 
       $this->row_count = (isset($users))? count($users):0; 

       // Return if users are available 
       return (isset($users))? $users:0; 
      } 

     public function Record($_user) 
      { 
       $connected = $this->db->con->prepare("INSERT INTO members_connected (nom, timestamp) VALUES (:nom,NOW()) ON DUPLICATE KEY UPDATE timestamp = NOW()"); 
       $connected->bindParam(':nom', $_user); 
       $connected->execute(); 
      } 
    } 


include('class.connect.php'); 
include('class.user.php'); 

$db  = new DBEngine(); 

if(isset($_SESSION['nom'])) { 
     // Start the timestamp, feed database 
     $u_check = new TimeStamp($db); 

     // Record into db 
     $u_check->Record($_SESSION['nom']); 

     // Check db, print users 
     $users = $u_check->Fetch($_SESSION['nom']); 
     print_r($users); 

     // Display count 
     echo "USERS: ".$u_check->row_count; 
    } 
2

두 번째 포함 후에 다음을 추가하십시오.

$db = new DBEngine(); 

그럼 당신은 당신이를 설정해야처럼 두 번째 오류에 대한 답변에서 UPDATE

$db->con->prepare(... 

에 보이는 것을

$this->db->con->prepare(... 

변화가 코드에 바인딩의 변수 유형

$log->bindParam(1, $_SESSION['name'], PDO::PARAM_STR, 25); 
$log->bindParam(2, time(), PDO::PARAM_STR, 20); 

또는, 그런 변수를 결합 할 필요가 없습니다 당신은

$log->execute(array($_SESSION['name'], time())); 
+0

그럼 내가 가진 여러 에러를 정정했는데, 나는 아직 갖고 싶지 않지만 고맙다! – Kuma

관련 문제