具有IP信息的页面命中计数器

时间:2018-04-09 21:26:16

标签: php sql

我试图让我自己的页面点击计数器,但遇到了一些错误。

首先我收到错误:

警告:mysqli_num_rows()要求参数1为mysqli_result,第21行给出布尔值

与此代码相关:

<?php
// REQUIRE DB CONNECTION
$host = "host"; // Host name
$username = "username"; // Mysql username
$password = "password"; // Mysql password
$db_name = "database"; // Database name
$tbl_name = "info"; // Table name
$tbl_name2 = "hits"; // Table name

// Connect to server and select databse.
$conn = mysqli_connect($host, $username, $password)or die("cannot connect"); 
mysqli_select_db($conn, $db_name)or die("cannot select DB");

// CHECK IF PAGE EXISTS IN PAGE HIT TABLE
function checkPageName($page_name) {

    global $conn, $tbl_name2;

    $sql = "SELECT * FROM $tbl_name2 WHERE page = $page_name";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) == 0) {
        $sql = "INSERT INTO $tbl_name2 (page, count) VALUES (:page, 0)";
        $result = mysqli_query($conn, $sql);
    }
}

// UPDATE PAGE HIT COUNT
function updateCounter($page_name) {

    global $conn, $tbl_name2;

    checkPageName($page_name);
    $sql = "UPDATE $tbl_name2 SET count = count+1 WHERE page = $page_name";
    $result = mysqli_query($conn, $sql);
}

// UPDATE VISITOR INFO
function updateInfo() {

    global $conn, $tbl_name;

    $ip_address = $_SERVER['REMOTE_ADDR'];
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    date_default_timezone_set('Europe/London');
    $datetime = date("d/m/y H:i:s"); // create date and time
    $sql = "INSERT INTO $tbl_name (ip_address, user_agent, time_accessed) VALUES($ip_address, $user_agent, $datetime)";
    $result = mysqli_query($conn, $sql);
}
?>

表信息包含访问的IP地址,用户代理和时间,命中表包含页面名称和命中数。

我在要跟踪的每个页面上调用这些函数。例如,我想跟踪我添加的主页:

updateCounter("Home");updateInfo();

我通过刷新页面测试了这段代码,现在它根本没有更新任何内容

0 个答案:

没有答案