2

我正在尝试使用 Highcharts 显示每个属性的用户访问,但是我不确定如何设置 SQL 查询以正确显示它。如果我们的数据库仅列出特定时间段内的页面访问量会容易得多,但它是按用户、按属性、每次访问存储的,如您在此处看到的:

数据

我知道我必须查询每个属性的总访问量并按月排序,但最好的方法是我无法做到这一点。这是我到目前为止所得到的:

    <?php

$con = mysql_connect('localhost', 'root', 'root');
if (!$con) {
  die('Could not connect: '.mysql_error());
}

mysql_select_db('demeure', $con);

//$date_start = $_POST['date_start'];
//$date_end = $_POST['date_end'];
$date_start = '2012-07-01 00:00:00';
$date_end = '2012-08-01 00:00:00';

$result = mysql_query("
  SELECT
    COUNT(id) AS count,
    created_at
  FROM
    user_property_visits
  WHERE
    created_at BETWEEN '$date_start' AND '$date_end'");

while ($row = mysql_fetch_array($result)) {
  echo $row['count'] . "\t" . $row['created_at']. "\n";
}

mysql_close($con);

?>

这会导致空白页。我不确定我离得到我需要的东西有多近,但感谢您的帮助。

4

2 回答 2

0

我不确定你是否想要这个,但你可以过滤子句中的WHERE句点。检查它是否有帮助:

<?php

// Show all PHP errors.
error_reporting(E_ALL);

// Connect with MySQL
$con = mysql_connect('localhost', 'your_user', 'your_password');
if (!$con) {
  die('Could not connect: '.mysql_error());
}
mysql_select_db('demeure', $con);

// Obtain date range from $_POST or
// manually edit this.

//$date_start = $_POST['date_start'];
//$date_end = $_POST['date_end'];
$date_start = '2012-07-20 20:30:00';
$date_end = '2012-07-20 20:37:00';

// Query with GROUP BY
$result = mysql_query("
  SELECT
    COUNT(id) AS `count`,
    created_at
  FROM
    user_property_visits
  WHERE
    created_at BETWEEN '$date_start' AND '$date_end'
  GROUP BY
    created_at");

while ($row = mysql_fetch_array($result)) {
  echo 'Count: ' . $row['count'] . '<br/>' . 'Group by date: ' . $row['created_at'] . '<br/>';
}

// With the schema used in SQL Fiddle, this code will reproduce
//
// A PHP Error was encountered
// Severity: 8192
// Message: mysql_connect(): The mysql extension is deprecated and
// will be removed in the future: use mysqli or PDO instead
//
// Filename: views/home.php
//
// Line Number: 7
//
// Count: 4
// Group by date: 2012-07-20 20:36:28

mysql_close($con);

?>

使用它,您可以使用日期选择器,例如,选择日期范围并通过 POST (ajax) 获取此值。

检查这个 SQL Fiddle

于 2013-10-26T17:32:15.993 回答
0

我能够通过以下方式解决它:

    <?php

$host = "localhost";
$user = "root";
$pass = "root";
$database = "demeure_new";

$dsn = "mysql:dbname=$database;host=$host";
$pdo = new PDO($dsn, $user, $pass);

$query = "SELECT DAY(created_at) AS day, MONTH(created_at) AS month, YEAR(created_at) AS year, count(user_id) AS count FROM user_property_views GROUP BY day";
$stmt = $pdo->prepare($query);
$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();

$newArray = array();
foreach($result as $r)
  $newArray[$r["year"]][$r["month"]][$r["day"]] = $r["count"];

//print_r($newArray);

json_encode($result, JSON_NUMERIC_CHECK);

echo json_encode($newArray);
?>
于 2013-10-26T20:36:47.180 回答