Change Value of Static Property of a Class Using Setters and Getters - php

I want to extend this class (which I downloaded) to suit my own needs. When I run call this class I get an error complaining that there is an unexpected = in the constructor.
define("HIT_OLD_AFTER_SECONDS", 4 * 7 * 24 * 3600);
class PHPCount extends DatabaseObject {
protected static $table_name = "hits";
protected static $db_fields = array('pageid','isunique', 'hitcount','english_id');
public $pageid;
public $isunique;
public $hitcount;
public $english_id;
public static $article_id;
function __construct(){
return PHPCount::article_id = PHPCount::get_article_id();
}
public static function set_article_id($articleid){
PHPCount::article_id = $articleid;
}
public static function get_article_id(){
return PHPCount::article_id;
}
public static function AddHit($pageID, $visitorID){
HitTest::Cleanup();
self::CreateCountsIfNotPresent($pageID);
if(HitTest::UniqueHit($pageID, $visitorID)){
self::CountHit($pageID, true);
HitTest::LogHit($pageID, $visitorID);
}
self::CountHit($pageID, false);
}
/*
* Returns (int) the amount of hits a page has
* $pageID - the page identifier
* $unique - true if you want unique hit count
*/
public static function GetHits($pageID, $unique = false){
global $database;
self::CreateCountsIfNotPresent($pageID);
$pageID = $database->escape_value($pageID);
$unique = $unique ? '1' : '0';
$q = "SELECT hitcount FROM hits WHERE ";
$q .= get_article_id()=$pageID;
$q .=" AND isunique={$unique}";
$getHitCount = static::find_by_sql($q);
if(sizeof($getHitCount) >= 1){
foreach($getHitCount as $hit){
return (int)$hit->hitcount;
}
}else{
die("Fatal: Missing hit count from database!");
}
}
/*
* Returns the total amount of hits to the entire website
* When $unique is FALSE, it returns the sum of all non-unique hit counts
* for every page. When $unique is TRUE, it returns the sum of all unique
* hit counts for every page, so the value that's returned IS NOT the
* amount of site-wide unique hits, it is the sum of each page's unique
* hit count.
*/
public static function GetTotalHits($unique = false){
//global $phpcount_con;
$total = 0;
$unique = $unique ? '1' : '0';
$q = "SELECT hitcount FROM hits WHERE isunique={$unique}";
$count = static::find_by_sql($q);
foreach($count as $hit){
$total += (int)$hit->hitcount;
}
return $total;
}
private static function CountHit($pageID, $unique){
global $database;
$unique = $unique ? '1' : '0';
$safeID = $database->escape_value($pageID);
$q ="UPDATE hits SET hitcount = hitcount + 1 WHERE ";
$q .=get_article_id()=$safeID;
$q .=" AND isunique={$unique}";
mysqli_query($database->connection,$q);
}
private static function CreateCountsIfNotPresent($pageID){
global $database;
$pageID = $database->escape_value($pageID);
$q = "SELECT pageid FROM hits WHERE ";
$q .=get_article_id()=$pageID;
$q .=" AND isunique='0'";
$createCount = static::find_by_sql($q);
if($q === false || sizeof($createCount) < 1){
$sql ="INSERT INTO hits(";
$sql .=get_article_id();
$sql .=", isunique, hitcount) VALUES(";
$sql .=$pageID;
$sql .=", '0', '0')";
mysqli_query($database->connection,$sql);
}
//check unique row
$q ="SELECT "get_article_id();
$q .=" FROM hits WHERE ";
$q .=get_article_id()=$pageID;
$q .=" AND isunique='1'";
$createCount = static::find_by_sql($q);
if($q === false || sizeof($createCount) < 1){
$sql ="INSERT INTO hits (";
$sql .=get_article_id();
$sql .=", isunique, hitcount) VALUES('$pageID', '1', '0')"
mysqli_query($database->connection,$sql);
echo mysqli_error($database->connection);
}
}
}

Access to static class variables require a $-sign like here:
PHPCount::$article_id
Thus, at least these methods need to be changed.
// I'd propose to pass the article ID as a parameter here
function __construct( $theArticleID ){
PHPCount::$article_id = $theArticleID;
}
public static function set_article_id($articleid){
PHPCount::$article_id = $articleid;
}
public static function get_article_id(){
return PHPCount::$article_id;
}

Related

how to do Pagination connection with DB static Connection

i have created pagination for my product page! while write down code. i have got this error
Error code image screenshot
Strict standards: Non-static method DB_Connection::obj_db() should not be called statically in D:\wamp\www\project\ali\models\product.php on line 147
require_once 'db_connection.php';
require_once 'brand.php';
class Product extends DB_Connection{
public $productID;
public $product_name;
public $description;
public $product_features;
public $unit_price;
public $quantity;
public $view_count;
public $product_image;
public $featured;
public $brand;
public function __construct() {
$this->brand = new Brand();//composition
}
public function get_product($view_update = FALSE) {
$obj_db = $this->obj_db();
$query = "select * "
. "from products_full "
. "where productID = '$this->productID'";
$result = $obj_db->query($query);
if($obj_db->errno){
throw new Exception("Select product Error - $obj_db->error -$obj_db->errno");
}
if($result->num_rows == 0){
throw new Exception("Product not found");
}
$data = $result->fetch_object();
if($view_update){
$data->view_count++;
$query_update = "update products set "
. "view_count = view_count+1 "
. "where `productID` = '$this->productID'";
$r2 = $obj_db->query($query_update);
if($obj_db->affected_rows == 0){
}
}
$this->productID = $data->productID;
$this->product_name = $data->product_name;
$this->description = $data->description;
$this->product_features = $data->product_features;
$this->product_image = $data->product_image;
$this->unit_price = $data->unit_price;
$this->quantity = $data->quantity;
$this->view_count = $data->view_count;
$this->brand->brandID = $data->brandID;
$this->brand->brand_name = $data->brand_name;
$this->brand->brand_image = $data->brand_image;
}
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {
//$obj_db = $this->obj_db();
$obj_db = self::obj_db();
$query = "select * from `products` ";
if($brandID > 0){
$query .= " where brandID = $brandID";
}
$types = array("all", "top", "new");
if(!in_array($type, $types)){
$type = "all";
}
if($type == "top"){
$query .= " order by view_count desc";
}
if($type == "new"){
$query .= " order by productID desc";
}
if($start > -1 && $count > 0){
$query .= " limit $start, $count";
}
$result = $obj_db->query($query);
if($obj_db->errno){
throw new Exception("Select products Error - $obj_db->error -$obj_db->errno");
}
if($result->num_rows == 0){
throw new Exception("Product(s) not found");
}
$products = array();
while($data = $result->fetch_object()){
$temp = new Product();
$temp->productID = $data->productID;
$temp->product_name = $data->product_name;
$temp->description = $data->description;
$temp->product_features = $data->product_features;
$temp->product_image = $data->product_image;
$temp->unit_price = $data->unit_price;
$temp->quantity = $data->quantity;
$temp->view_count = $data->view_count;
//$temp->brand = TRUE;
$products[] = $temp;
}
return $products;
}
public static function pagination($item_per_page = 6, $brandID = 0){
$obj_db = self::obj_db();
$query = "select count(*) 'count' from `products`";//alias
if($brandID > 0){
$query .= " where brandID = $brandID";
}
$result = $obj_db->query($query);
if($result->num_rows == 0){
throw new Exception("Product(s) not found");
}
$data = $result->fetch_object();
$total_items = $data->count;
$page_count = ceil($total_items/$item_per_page);
$page_nums = array();
for($i = 1, $j = 0 ; $i <= $page_count; $i++, $j+=$item_per_page){
$page_nums[$i] = $j;
}
return $page_nums;
}
First let me give you an example and explain why this is happening.
class A{
public function foo(){
echo "foo";
}
public static function bar(){
// If you call the foo function like this:
// error: Strict standards: Non-static method A::foo() should not be called statically
//self::foo();
// That means it should be called like this:
(new A)->foo();
}
}
A::bar();
The difference between static and non-static is that the first one doesn't need initialization so you can call the classname then append :: to it and call the method immediately. like this:
ClassName::method();
and if the method is not static you need to initialize it like this:
$obj = new ClassNmae();
$onj->method();
However in PHP 5.4 you can use this syntax instead for faster calling:
(new ClassName)->method();
That being said, this is what you need to change in your code:
// your code
public function get_product($view_update = FALSE) {
$obj_db = $this->obj_db();
// your code
}
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {
$obj_db = (new DB_Connection)->obj_db();
// your code
}
public static function pagination($item_per_page = 6, $brandID = 0){
$obj_db = (new DB_Connection)->obj_db();
// your code
}
i had done mistake, i have created static function for pagination. i have forgot to add static on db connection page!
protected static function obj_db(){
and
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {
//$obj_db = $this->obj_db();
$obj_db = self::obj_db();
thanks all of them, who have guide me as well!

Php Error. Notice: Undefined property: mysqliConn::$affected_rows

I have this code in OOP Php
include ('connection.php');
class NestedSet
{
/*Properties*/
/**
* Mysqli object
* #var object
*/
protected $db;
/**
* Name of the database table
* #var string
*/
public $table = 'tree';
/**
* Primary key of the database table
* #var string
*/
public $pk = 'id';
/**
* Namefield in the database table
* #var unknown_type
*/
public $name = 'name';
/*Methods*/
/**
* Stores a Mysqli object for further use
* #param object $mysqli Mysqli object
* #return boolean true
*/
public function __construct() {
$this->db = mysqliConn::init();
return true;
}
protected static $instance = NULL;
// public static function get_instance()
// {
// //if ( NULL === self::$instance )
// // self::$instance = new self;
// // return self::$instance;
// }
/**
* Creates the root node
* #param string $name Name of the new node
* #return boolean true
*/
public function createRootNode($name) {
$this->db->query("LOCK TABLES " . $this->table . " WRITE");
$sql = "SELECT rgt FROM " . $this->table . " ORDER BY rgt DESC LIMIT 1";
$result = $this->db->query($sql);
if ($this->db->affected_rows == 0) {
$lft = 1;
$rgt = 2;
} else {
$obj = $result->fetch_object();
$lft = $obj->rgt + 1;
$rgt = $lft + 1;
}
$sql = "INSERT INTO " . $this->table . " (" . $this->name . ", lft, rgt) VALUES ('" . $name . "', " . $lft . ", " . $rgt . ");";
$this->db->query($sql);
$this->db->query("UNLOCK TABLES");
return true;
}
}
?>
I create a new object for the class NestedSet in an other file called index.php
<?php
include("nested_set.php");
$nested = new NestedSet(); //Create a NestedSet object
$nested->createRootNode('root');
?>
I can write on db but the $rgt and $lft stays 2 and 1;
and this error is displayd :
"Notice: Undefined property: mysqliConn::$affected_rows in C:\wamp\www\hr-test\nested_set.php on line 67"
Any idea on what im doing wrong?
Thank you!!
CODE FOR connection.php
<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'hr_test2');
class mysqliConn
{
private static $instance;
private $connection;
private function __construct()
{
$this->connection = new mysqli(SERVER,USERNAME,PASSWORD,DATABASE);
}
public static function init()
{
if(is_null(self::$instance))
{
self::$instance = new mysqliConn();
}
return self::$instance;
}
public function __call($name, $args)
{
if(method_exists($this->connection, $name))
{
return call_user_func_array(array($this->connection, $name), $args);
} else {
trigger_error('Unknown Method ' . $name . '()', E_USER_WARNING);
return false;
}
}
}
?>
Because a mysqli->query() returns a mysqli_result object which will contain information about the result of the query you need to use $result and not $this->db->
Also the mysqli_result object does not contain an affected_rows property you should use the num_rows property which does exist, but on the $result object instead.
You can also simplify the concatenation of the query string you create, although you should really use prepared statements.
public function createRootNode($name) {
$this->db->query("LOCK TABLES " . $this->table . " WRITE");
$sql = "SELECT rgt FROM " . $this->table . " ORDER BY rgt DESC LIMIT 1";
$result = $this->db->query($sql);
// if ($this->db->affected_rows == 0) {
if ($result->num_rows == 0) {
$lft = 1;
$rgt = 2;
} else {
$obj = $result->fetch_object();
$lft = $obj->rgt + 1;
$rgt = $lft + 1;
}
$sql = "INSERT INTO {$this->table} ( {$this->name}, lft, rgt)
VALUES ('$name', $lft , $rgt)";
$this->db->query($sql);
$this->db->query("UNLOCK TABLES");
return true;
}

CRUD - Error When Updating MySQL Database

I'm learning how to code php and I'm working on the CRUD. So far, I've got the Create and Delete methods working, but for the life of me, I cannot get Update to work. Am I missing something completely obvious here? Thank you in advance.
In my User class I have the following (Only related methods included here):
protected static $table_name="users";
protected static $db_fields = array('id', 'f_name');
public $id;
public $f_name;
public static function find_by_id($id=0) {
global $db;
$result_array = self::find_by_sql("SELECT * FROM ".static::$table_name." WHERE id={$id} LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false;
}
protected function attributes() {
$attributes = array();
foreach(self::$db_fields as $field) {
if(property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
protected function sanitized_attributes() {
global $db;
$clean_attributes = array();
foreach($this->attributes() as $key => $value){
$clean_attributes[$key] = $db->escape_value($value);
}
return $clean_attibutes;
}
public function update() {
global $db;
$attributes = $this->attributes();
$attribute_pairs = array();
foreach($attributes as $key => $value) {
$attibute_pairs[] = "{$key}='{$value}'";
}
$sql = "UPDATE ".self::$table_name." SET ";
$sql .= join(", ", $attribute_pairs);
$sql .= " WHERE id=". $db->escape_value($this->id);
$db->query($sql);
if ($db->affected_rows() == 1) {
echo "Yes!";
} else {
echo "No!";
}
}
In my html file, I simply have:
<?php
$user = User::find_by_id(1);
$user->f_name = "UpdatedUser";
$user->update();
?>
Well for one, your query is probably failing on account of this
$sql = "UPDATE ".static::$table_name." SET ";
being invalid. Try:
$sql = "UPDATE ".self::$table_name." SET ";

PDO concatenating SQL Query with placeholders

I'm in the process of converting my mysql_.* functions to PDO. Prior to this, I would build my query by concatenating it based on other factors. Here's an example:
class carriersInfo
{
protected $start_date;
protected $end_date;
protected $data = array();
public function __construct($start = 0, $end = 0)
{
$this->start_date = $start;
$this->end_date = $end;
$this->topCarrier();
}
protected function buildQ()
{
$sql = "SELECT `shipped_by`, COUNT(`shipped_by`) AS `total` FROM `deliveries` ";
if($this->start_date!=0 and $this->end_date!=0)
{
$sql .= " WHERE ship_date>='".$this->start_date."' AND ship_date<='".$this->end_date."' ";
}
$sql .= " GROUP BY `shipped_by` ";
$sql .= " ORDER BY total ASC ";
return $sql;
}
public function topCarrier()
{
$query = $this->buildQ();
$SQL = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_array($SQL))
{
$this->data[$data['shipped_by']] = $data['total'];
}
return $this->data;
}
}
The SQL query was build depending if the user chose to specify a date range. How can I achieve the same effect using PDO prepared statements and nameplaceholders?
Attempt
class carriersInfo
{
protected $start_date;
protected $end_date;
protected $data = array();
protected $_INSTANCE;
public function __construct($start = 0, $end = 0)
{
$this->_INSTANCE = Core::getInstance();
$this->start_date = $start;
$this->end_date = $end;
$this->topCarrier();
}
protected function buildQ()
{
$sql = "SELECT `shipped_by`, COUNT(`shipped_by`) AS `total` FROM `deliveries` ";
if($this->start_date!=0 and $this->end_date!=0)
{
$sql .= " WHERE ship_date>=:start_date AND ship_date<=:end_date ";
}
$sql .= " GROUP BY `shipped_by` ";
$sql .= " ORDER BY total ASC ";
return $sql;
}
public function topCarrier()
{
$Q = $this->buildQ();
$query = $this->_INSTANCE->pdo->prepare($Q);
$query->bindValue(":start_date",$this->start_date);
$query->bindValue(":end_date",$this->end_date);
$query->execute();
while($data = $query->fetch())
{
$this->data[$data['shipped_by']] = $data['total'];
}
return $this->data;
}
}
The binding parameters won't work in this case if the user did not enter a date range and would cause an error.
If I understand you correctly, attempting to bind parameters that don't exist is causing your query to fail. Based on that, you should just add a check for those properties:
public function topCarrier()
{
$Q = $this->buildQ();
$query = $this->_INSTANCE->pdo->prepare($Q);
if($this->start_date!=0 and $this->end_date!=0) {
$query->bindValue(":start_date",$this->start_date);
$query->bindValue(":end_date",$this->end_date);
}
$query->execute();
while($data = $query->fetch())
{
$this->data[$data['shipped_by']] = $data['total'];
}
return $this->data;
}
Figured it out. Didn't know you can execute an array of parameters. Here's my solution.
class carriersInfo
{
protected $start_date;
protected $end_date;
protected $data = array();
protected $_INSTANCE;
protected $_PARAMETERS = array();
public function __construct($start = 0, $end = 0)
{
$this->_INSTANCE = Core::getInstance();
$this->start_date = $start;
$this->end_date = $end;
$this->topCarrier();
}
protected function buildQ()
{
$sql = "SELECT `shipped_by`, COUNT(`shipped_by`) AS `total` FROM `deliveries` ";
if($this->start_date!=0 and $this->end_date!=0)
{
$sql .= " WHERE ship_date>=:start_date AND ship_date<=:end_date ";
$this->_PARAMETERS[":start_date"] = $this->start_date;
$this->_PARAMETERS[":end_date"] = $this->end_date;
}
$sql .= " GROUP BY `shipped_by` ";
$sql .= " ORDER BY total ASC ";
return $sql;
}
public function topCarrier()
{
$Q = $this->buildQ();
$query = $this->_INSTANCE->pdo->prepare($Q);
$query->execute($this->_PARAMETERS);
while($data = $query->fetch())
{
$this->data[$data['shipped_by']] = $data['total'];
}
return $this->data;
}
}

PHP page view counter

I downloaded a php view counter and I set it the database but when it comes to show the hits I am getting error. The documentation says
USAGE:
In your script, use reqire_once() to import this script, then call the
functions like PHPCount::AddHit(...); See each function for help.
*
Here is my demo.php where I want to show the view. But it is throwing
Notice: Undefined variable: pageID in C:\xampp\htdocs\test\demo.php on line 4
1
<?php
include_once("hit.php");
echo //this is what I added.
PHPCount::AddHit($pageID);
?>
The below is hit.php and what I wanna know is how can I show the views on the above demo.php?
<?php
/*
* USAGE:
* In your script, use reqire_once() to import this script, then call the
* functions like PHPCount::AddHit(...); See each function for help.
*
* NOTE: You must set the database credentials in the InitDB method.
*/
class PHPCount
{
/*
* Defines how many seconds a hit should be rememberd for. This prevents the
* database from perpetually increasing in size. Thirty days (the default)
* works well. If someone visits a page and comes back in a month, it will be
* counted as another unique hit.
*/
const HIT_OLD_AFTER_SECONDS = 2592000; // default: 30 days.
// Don't count hits from search robots and crawlers.
const IGNORE_SEARCH_BOTS = true;
// Don't count the hit if the browser sends the DNT: 1 header.
const HONOR_DO_NOT_TRACK = false;
private static $IP_IGNORE_LIST = array(
'127.0.0.1',
);
private static $DB = false;
private static function InitDB()
{
if(self::$DB)
return;
try
{
// TODO: Set the database login credentials.
self::$DB = new PDO(
'mysql:host=localhost;dbname=test',
'root', // Username
'', // Password
array(PDO::ATTR_PERSISTENT => true)
);
}
catch(Exception $e)
{
die('Failed to connect to phpcount database');
}
}
/*
* Adds a hit to a page specified by a unique $pageID string.
*/
public static function AddHit($pageID)
{
if(self::IGNORE_SEARCH_BOTS && self::IsSearchBot())
return false;
if(in_array($_SERVER['REMOTE_ADDR'], self::$IP_IGNORE_LIST))
return false;
if(
self::HONOR_DO_NOT_TRACK &&
isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] == "1"
) {
return false;
}
self::InitDB();
self::Cleanup();
self::CreateCountsIfNotPresent($pageID);
if(self::UniqueHit($pageID))
{
self::CountHit($pageID, true);
self::LogHit($pageID);
}
self::CountHit($pageID, false);
return true;
}
/*
* Returns (int) the amount of hits a page has
* $pageID - the page identifier
* $unique - true if you want unique hit count
*/
public static function GetHits($pageID, $unique = false)
{
self::InitDB();
self::CreateCountsIfNotPresent($pageID);
$q = self::$DB->prepare(
'SELECT hitcount FROM hits
WHERE pageid = :pageid AND isunique = :isunique'
);
$q->bindParam(':pageid', $pageID);
$q->bindParam(':isunique', $unique);
$q->execute();
if(($res = $q->fetch()) !== FALSE)
{
return (int)$res['hitcount'];
}
else
{
die("Missing hit count from database!");
return false;
}
}
/*
* Returns the total amount of hits to the entire website
* When $unique is FALSE, it returns the sum of all non-unique hit counts
* for every page. When $unique is TRUE, it returns the sum of all unique
* hit counts for every page, so the value that's returned IS NOT the
* amount of site-wide unique hits, it is the sum of each page's unique
* hit count.
*/
public static function GetTotalHits($unique = false)
{
self::InitDB();
$q = self::$DB->prepare(
'SELECT hitcount FROM hits WHERE isunique = :isunique'
);
$q->bindParam(':isunique', $unique);
$q->execute();
$rows = $q->fetchAll();
$total = 0;
foreach($rows as $row)
{
$total += (int)$row['hitcount'];
}
return $total;
}
/*====================== PRIVATE METHODS =============================*/
private static function IsSearchBot()
{
// Of course, this is not perfect, but it at least catches the major
// search engines that index most often.
$keywords = array(
'bot',
'spider',
'spyder',
'crawlwer',
'walker',
'search',
'yahoo',
'holmes',
'htdig',
'archive',
'tineye',
'yacy',
'yeti',
);
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
foreach($keywords as $keyword)
{
if(strpos($agent, $keyword) !== false)
return true;
}
return false;
}
private static function UniqueHit($pageID)
{
$ids_hash = self::IDHash($pageID);
$q = self::$DB->prepare(
'SELECT time FROM nodupes WHERE ids_hash = :ids_hash'
);
$q->bindParam(':ids_hash', $ids_hash);
$q->execute();
if(($res = $q->fetch()) !== false)
{
if($res['time'] > time() - self::HIT_OLD_AFTER_SECONDS)
return false;
else
return true;
}
else
{
return true;
}
}
private static function LogHit($pageID)
{
$ids_hash = self::IDHash($pageID);
$q = self::$DB->prepare(
'SELECT time FROM nodupes WHERE ids_hash = :ids_hash'
);
$q->bindParam(':ids_hash', $ids_hash);
$q->execute();
$curTime = time();
if(($res = $q->fetch()) !== false)
{
$s = self::$DB->prepare(
'UPDATE nodupes SET time = :time WHERE ids_hash = :ids_hash'
);
$s->bindParam(':time', $curTime);
$s->bindParam(':ids_hash', $ids_hash);
$s->execute();
}
else
{
$s = self::$DB->prepare(
'INSERT INTO nodupes (ids_hash, time)
VALUES( :ids_hash, :time )'
);
$s->bindParam(':time', $curTime);
$s->bindParam(':ids_hash', $ids_hash);
$s->execute();
}
}
private static function CountHit($pageID, $unique)
{
$q = self::$DB->prepare(
'UPDATE hits SET hitcount = hitcount + 1 ' .
'WHERE pageid = :pageid AND isunique = :isunique'
);
$q->bindParam(':pageid', $pageID);
$unique = $unique ? '1' : '0';
$q->bindParam(':isunique', $unique);
$q->execute();
}
private static function IDHash($pageID)
{
$visitorID = $_SERVER['REMOTE_ADDR'];
return hash("SHA256", $pageID . $visitorID);
}
private static function CreateCountsIfNotPresent($pageID)
{
// Non-unique
$q = self::$DB->prepare(
'SELECT pageid FROM hits WHERE pageid = :pageid AND isunique = 0'
);
$q->bindParam(':pageid', $pageID);
$q->execute();
if($q->fetch() === false)
{
$s = self::$DB->prepare(
'INSERT INTO hits (pageid, isunique, hitcount)
VALUES (:pageid, 0, 0)'
);
$s->bindParam(':pageid', $pageID);
$s->execute();
}
// Unique
$q = self::$DB->prepare(
'SELECT pageid FROM hits WHERE pageid = :pageid AND isunique = 1'
);
$q->bindParam(':pageid', $pageID);
$q->execute();
if($q->fetch() === false)
{
$s = self::$DB->prepare(
'INSERT INTO hits (pageid, isunique, hitcount)
VALUES (:pageid, 1, 0)'
);
$s->bindParam(':pageid', $pageID);
$s->execute();
}
}
private static function Cleanup()
{
$last_interval = time() - self::HIT_OLD_AFTER_SECONDS;
$q = self::$DB->prepare(
'DELETE FROM nodupes WHERE time < :time'
);
$q->bindParam(':time', $last_interval);
$q->execute();
}
}
You may need to specify the PageID
PHPCount::AddHit("index");

Categories