我曾经用旧的方式在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 ',';
}
}
}
}
&GT;
`
有什么想法吗?
答案 0 :(得分:2)
尝试在第二次加入后添加以下内容;
$db = new DBEngine();
然后在您拥有的代码中
$this->db->con->prepare(...
将其更改为
$db->con->prepare(...
<强>更新强> 在回答第二个错误时,您似乎需要在绑定中设置变量类型。
e.g。
$log->bindParam(1, $_SESSION['name'], PDO::PARAM_STR, 25);
$log->bindParam(2, time(), PDO::PARAM_STR, 20);
或者,您不需要像这样绑定变量,您可以将它们直接传递到execute
,就像这样(这是您在第一个查询中所拥有的)btw
$log->execute(array($_SESSION['name'], time()));
答案 1 :(得分:1)
创建一个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;
}