php mysqli тоолуур

Posted: 4 сар 12, 2011 in Проргамчлал, mysql, php

ene mysql deerh database uusgeh helber

CREATE TABLE  `mytable`.`mytable` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`views` BIGINT NOT NULL ,
`post_id` INT NOT NULL,
`date` date
) ENGINE = INNODB;

database shud uusgeh code

CREATE TABLE IF NOT EXISTS `mytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `views` bigint(20) NOT NULL,
  `post_id` int(11) NOT NULL,
  `date` date,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

ene ehled databas dere neg utga insert higerei post_id = 1 views = uzsen toog hedes ch ehlulj bolno

<?php
// Define constants here
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'pass');
define('DB_NAME', 'mydata');
?>

<?php
class b_class {
	private $conn;
	public $table = "mytable";
	function __construct() {
		$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or
					  die('1There was a problem connecting to the database.');
}
	function insert_user_views($post_id){

		$query = "insert into ".$this->table." (post_id, views, date) values (?, 1, now())";

		if($stmt = $this->conn->prepare($query)) {
			$stmt->bind_param('s', $post_id);
			$stmt->execute();
			$stmt->close();
		}
	}

	function call_user_views_old($interval) {

		$query = "SELECT count(views) FROM ".$this->table." where date >= DATE_SUB(CURDATE(), INTERVAL ? DAY)";

		if($stmt = $this->conn->prepare($query)) {
			$stmt->bind_param('i',$interval);
			$stmt->execute();
			$stmt->bind_result($views);
			if($stmt->fetch()){
				return $views;
			}

		}
	}	

//class tugsgul
}
?>

<?php 
$bclass = new b_class();
$post_id =1;
?>
<?php $bclass->insert_user_views($post_id); // index huudas deer bairluulah function ?>
<div>Өнөөдөр <?php echo $bclass->call_user_views_old(1);
//() hashilt dotor udur-n toog bichne 1 honog 7 honog 30 honog gehmet  ?>
 - удаа үзсэн</div>
<div>Энэ 7 хоногт <?php echo $bclass->call_user_views_old(7); ?> - удаа үзсэн</div>
<div>Энэ сард <?php echo $bclass->call_user_views_old(30); ?> - удаа үзсэн</div>



Advertisements

Хариулт үлдээх

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Өөрчлөх )

Twitter picture

You are commenting using your Twitter account. Log Out / Өөрчлөх )

Facebook photo

You are commenting using your Facebook account. Log Out / Өөрчлөх )

Google+ photo

You are commenting using your Google+ account. Log Out / Өөрчлөх )

Connecting to %s