2013-06-24 74 views
0

我有一個複雜類型的數據庫,不幸的是,由於某種原因,我無法更改。如何在MySQL中使用UNION時進行分頁?

我必須在桌子之間強制UNION,並且必須顯示一張桌子。

我想爲表格做分頁,因爲它會返回一個大數據表。

但它不工作。

當我不使用UNION並從一個表中抓取數據時,這個代碼塊就起作用。

我需要一個解決方案。我在這個問題上遇到了很大的問題。請有人幫助我。

<?php 

conFunc($rootdb); // Connection Strings to Database 

$btsid = trim($_POST['btsid']); 
$date1 = $_POST['date1']; 
$date2 = $_POST['date2']; 

?> 

<b>From:</b> <?php echo $date1; ?> <br /> 
<b>To:</b> <?php echo $date2; ?><br /><br /> 

<?php 
$btsdb = mysql_query("SELECT * FROM `rollout_tracker` WHERE `site_id` LIKE '%".$btsid."'"); 
$rows = mysql_fetch_array($btsdb); 
?> 

<?php //----------------------------- PAGINATION (CHECK ROW) -----------------------------// 
// Don't Change [$result] Variable 
$result = mysql_query("SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_january` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_february` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_march` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_april` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_may` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_june` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_july` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_august` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_september` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_october` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_november` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_december` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id`"); 
//----------------------------- PAGINATION (CHECK ROW) -----------------------------// ?> 

<?php //----------------------------- PAGINATION START 1 -----------------------------// 

if (isset($_GET['pageno'])) 
{ 
    $pageno = $_GET['pageno']; 
} 
else 
{ 
    $pageno = 1; 
} 

$query_data = mysql_fetch_row($result); 
$numrows = $query_data[0]; 

$rows_per_page = 1; 
$lastpage = CEIL($numrows/$rows_per_page); 

$pageno = (int)$pageno; 
if ($pageno < 1) 
{ 
    $pageno = 1; 
} 
elseif ($pageno > $lastpage) 
{ 
    $pageno = $lastpage; 
} 

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 

//----------------------------- PAGINATION END 1 -----------------------------// ?> 

<?php //----------------------------- PAGINATION (GET DATA) -----------------------------// 

$result = mysql_query("SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_january` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_february` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_march` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_april` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_may` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_june` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_july` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_august` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_september` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_october` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_november` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           UNION 
           SELECT `user_id`, `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
           FROM `cdr_data_december` 
           WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
           GROUP BY `user_id` 
           ORDER BY `user_id` $limit"); 

echo "<h3 style=\"font-family:Verdana;color:black;\">Summary Report:</h3><br />"; 

echo "<table class=\"imagetable\" width='100%' border='1'><tr><th>User ID</th>"; 
if($_SESSION["type"]=="1") 
{ 
    echo "<th>MAC Address</th>"; 
} 
echo "<th>Total Connection Time (Minute)</th><th>Total Upload (MB)</th><th>Total Download (MB)</th><th>Detailed Usage</th></tr>"; 

while($row = mysql_fetch_array($result)) 
{ 
    if(($row['user_id'] != '') && (strpos($row['user_id'],'@') == false)) 
    { 
     echo "<tr align=\"center\">"; 
     echo "<td>"; 
     if($row["TotalUp"] >= ($row["TotalDown"] * (90/100))) 
     { 
      echo "<font color=\"red\">" . $row["user_id"] . "</font>"; 
     } 
     else 
     { 
      echo $row["user_id"]; 
     } 
     echo "</td>"; 
     if($_SESSION["type"]=="1") 
     { 
      echo "<td>"; 
       echo strtoupper($row["mac_add"]); 
      echo "</td>"; 
     } 
     echo "<td>"; 
      echo number_format($row["ConnTime"], 2, '.', ''); // $row["ConnTime"]; 
     echo "</td>"; 
     echo "<td>"; 
      echo number_format($row["TotalUp"], 2, '.', ''); // $row["TotalUp"]; 
     echo "</td>"; 
     echo "<td>"; 
      echo number_format($row["TotalDown"], 2, '.', ''); // $row["TotalDown"]; 
     echo "</td>"; 
     echo "<td>"; 
      echo "<a class='ajax' target=\"_blank\" title='[ Detailed Usage of the User ]' href='detailed_usage.php?sid=".$row['user_id']."&bid=".$btsid."&d1=".$date1."&d2=".$date2."&mac=".$row["mac_add"]."'><img height=\"12\" width=\"12\" src=\"gallery/file/edit-button.png\"></a>"; 
     echo "</td>"; 
     echo "</tr>"; 
    } 
} 
echo "</table><br/><br/>"; 
?> 

<?php //----------------------------- PAGINATION START 2 -----------------------------// 

if ($pageno == 1) 
{ 
    echo " FIRST PREV "; 
} 
else 
{ 
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> "; 
    $prevpage = $pageno-1; 
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> "; 
} 

echo " (Page $pageno of $lastpage) "; 

IF ($pageno == $lastpage) 
{ 
    echo " NEXT LAST "; 
} 
else 
{ 
    $nextpage = $pageno+1; 
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> "; 
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> "; 
} 

//----------------------------- PAGINATION END 2 -----------------------------// ?> 

</p> 

說明:未定義指數:btsid在d:\ XAMPP \ htdocs中\上線軟\ bts_usage_result.php 16

說明:未定義指數:在date1的d:\ XAMPP \ htdocs中\軟\ bts_usage_result.php上線17

說明:未定義指數:DATE2在d:\ XAMPP \ htdocs中\上線18

軟\ bts_usage_result.php來自:

收件人:

+0

警告:mysql_fetch_row()期望參數1是資源,在第117行D:\ XAMPP \ htdocs \ software \ bts_usage_result.php中給出的布爾值-------------和--- ---------警告:mysql_fetch_array()期望參數1是資源,在209行D:\ XAMPP \ htdocs \ software \ bts_usage_result.php中給出布爾值 – Nirjhor

+0

[mysql \ _fetch \ _array( )期望參數1是資源,布爾給出在選擇](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-選擇) – hjpotter92

+0

不!我不這麼認爲...並且,那個頁面不是我的問題的答案... – Nirjhor

回答

0

您在查詢結尾有一個額外的')'(或者它會出現)。刪除它並重試。

更重要的是 - 在嘗試使用數據之前,做一下@Bad Wolf所說的並檢查結果(和錯誤信息)。

+0

我已經刪除')',並試圖把mysql_error(),但沒有找到解決方案... – Nirjhor

+0

如果你把字符串(與適當的替代)放入mysql cmd行,你會得到什麼輸出? – ethrbunny

+0

我發現了一個問題點。我使用GROUP BY,因爲這個原因這個頁面顯示1行(這是正常的)。但處理程序頁面(上面給出的代碼)顯示「1-15」,因爲實際上有15行到來,在本頁面顯示爲1行,因爲我已經使用了GROUP BY。但在這種情況下,我應該使用什麼來分頁,我不理解... – Nirjhor