Created
August 19, 2017 11:04
-
-
Save Mauryashubham/14bff78571e87a62900c50a223cf45d1 to your computer and use it in GitHub Desktop.
Count Page views using PHP Mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 4.6.5.2 | |
-- https://www.phpmyadmin.net/ | |
-- | |
-- Host: 127.0.0.1 | |
-- Generation Time: Aug 19, 2017 at 01:01 PM | |
-- Server version: 10.1.21-MariaDB | |
-- PHP Version: 5.6.30 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `views` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `views` | |
-- | |
CREATE TABLE `views` ( | |
`views` bigint(20) NOT NULL DEFAULT '1', | |
`user_id` int(11) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Dumping data for table `views` | |
-- | |
INSERT INTO `views` (`views`, `user_id`) VALUES | |
(13, 1); | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `views` | |
-- | |
ALTER TABLE `views` | |
ADD PRIMARY KEY (`user_id`); | |
-- | |
-- AUTO_INCREMENT for dumped tables | |
-- | |
-- | |
-- AUTO_INCREMENT for table `views` | |
-- | |
ALTER TABLE `views` | |
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
Hi all , Welcome to shubhammaurya.com , Today we are going to discuss , | |
How to Count Page views using Mysql | |
<?php | |
$DB_host = "localhost"; | |
$DB_user = "root"; | |
$DB_pass = ""; | |
$DB_name = "views"; | |
try | |
{ | |
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass); | |
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// echo "Done.."; | |
} | |
catch(PDOException $e) | |
{ | |
echo "ERROR : ".$e->getMessage(); | |
} | |
//Fetch | |
$stmt_article=$DBcon->prepare("SELECT * FROM views"); | |
$stmt_article->execute(); | |
$article=$stmt_article->fetch(PDO::FETCH_ASSOC); | |
//echo $article['user_id']; | |
//Update View | |
$views=$article['views']; | |
$stmt_views=$DBcon->prepare("UPDATE views SET views=:views+1"); | |
$stmt_views->execute(array(':views'=>$views)); | |
echo "Page Views : ".$article['views']; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment