2013-10-26 70 views
2

我想使用Highcharts來顯示每個屬性的用戶訪問,但我不確定如何設置SQL查詢以正確顯示此。這將是一個容易得多,如果我們的數據庫只是簡單地列在特定的時間段訪問過的網頁,但它存儲在每個用戶,每個屬性,每次訪問,你可以在這裏看到:使用Highcharts與MySQL的頁面訪問

Data

我知道我必須查詢每個財產的總訪問次數並按月排序,但正是最好的方式來做這件事超出了我。這是我到目前爲止:

<?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); 

?> 

這結果與一個空白頁。我不確定我有多接近我需要的東西,但感謝您的幫助。

回答

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); 
?> 
+0

使用JSON代替,更好 –

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(阿賈克斯)獲得此值。

Check this SQL Fiddle

+0

這種替代查詢後,一個空白頁出現;我是否需要更改$ echo以便正確顯示它? –

+0

我已經把這個查詢。我將使用您發佈的完整代碼進行編輯。 – thiagobraga

+0

我在幾分鐘前看到了您的編輯並試圖使用它,但不幸的是它給了我一個空白頁面的相同結果。我試着複製並粘貼你寫的東西,只是替換了數據庫信息,但沒有運氣 –