簡體   English   中英

PHP中的SQL查詢

[英]sql queries in php

我經營着一個小型的服務台,我要求每個月向我的直屬經理制作一份工單報告。

我正在尋求升級生成此報告的方式,因為目前,我運行每月使用Navicat手動編寫的以下查詢:

 SELECT
 updates.incidentid AS `Incident ID`,
 updates.bodytext AS `Call Status`,
 updates.duration AS `Total Minutes`,
 software.`name` AS 'Support Type',
 incidents.title AS Description,
 contacts.forenames AS `First Name`,
 contacts.surname AS `Last Name`,
 FROM_UNIXTIME(incidents.opened, '%d.%m.%Y') AS `Date Logged`,
 sites.`name`,
 users.realname
 FROM
 updates
 INNER JOIN incidents ON updates.incidentid = incidents.id
 JOIN contacts ON incidents.contact = contacts.id
 INNER JOIN sites ON sites.id = contacts.siteid
 INNER JOIN users ON incidents.`owner` = users.id
 INNER JOIN software ON incidents.softwareid = software.id
 WHERE
 updates.bodytext = 'Incident Closed'
    AND FROM_UNIXTIME(incidents.opened, '%m') = '07'
            AND FROM_UNIXTIME(incidents.opened, '%Y') = '2016'
 ORDER BY contacts.siteid ASC

我想將所有內容放在一個非常簡單的網頁中,允許用戶選擇月份和年份,然后為他們提供HTML格式的下載。

我最好放在PHP中還是可以用HTML創建? 請客氣,我是一個新手,我的思維過程還差一點。

非常感謝您的耐心配合!

我將向您展示基本的內容:首先,一個HTML文件,用戶在其中輸入月份和年份作為數字(請注意對“ report.php”的調用):

<html>
  <body>
    <form method="post" action="report.php">
      Enter month (1..12) <input type="text" name="month"/>
      <br/>
      Enter year (four digits) <input type="text" name="year"/>
      <br/>
      <input type="submit" value="Display report"/>
    </form>
  </body>
</html>

現在,PHP文件“ report.php”(注意如何在開始時捕獲月份和年份並將其存儲在變量$month$year ):

<?php
$month = $_POST["month"]; // PARAMETERS FROM
$year  = $_POST["year"];  // THE HTML FILE.
$cnx = mysqli_connect( "localhost","user","password","databasename");
$data = mysqli_query( $cnx,"YOUR BIG SELECT HERE" ) or die( mysqli_error( $cnx ) );
echo "<table>" .
     "  <tr>" .
     "    <td>Incident id</td>" .
     "    <td>Call status</td>" .
     "    <td>Description</td>" .
     "    <td>Date logged</td>" .
     "    <td>Site name</td>" .
     "    <td>User</td>" .
     "  </tr>";
while ( $row = mysqli_fetch_array( $data ) ) // LOOP TO DISPLAY DATA.
  echo "<tr>" .
       "  <td>{$row["IncidentID"]}</td>" .
       "  <td>{$row["CallStatus"]}</td>" .
       "  <td>{$row["Description"]}</td>" .
       "  <td>{$row["DateLogged"]}</td>" .
       "  <td>{$row["name"]}</td>" .
       "  <td>{$row["realname"]}</td>" .
       "</tr>";
echo "</table>"; // TABLE END.
?>

您必須用大查詢替換文本“ YOUR BIG SELECT HERE”。 這是在查詢中插入變量$month$year的方式:

 SELECT
 updates.incidentid AS `Incident ID`,
 updates.bodytext AS `Call Status`,
 updates.duration AS `Total Minutes`,
 software.`name` AS 'Support Type',
 incidents.title AS Description,
 contacts.forenames AS `First Name`,
 contacts.surname AS `Last Name`,
 FROM_UNIXTIME(incidents.opened, '%d.%m.%Y') AS `Date Logged`,
 sites.`name`,
 users.realname
 FROM
 updates
 INNER JOIN incidents ON updates.incidentid = incidents.id
 JOIN contacts ON incidents.contact = contacts.id
 INNER JOIN sites ON sites.id = contacts.siteid
 INNER JOIN users ON incidents.`owner` = users.id
 INNER JOIN software ON incidents.softwareid = software.id
 WHERE
 updates.bodytext = 'Incident Closed'
    AND FROM_UNIXTIME(incidents.opened, '%m') = '$month'   ◄■■■■
            AND FROM_UNIXTIME(incidents.opened, '%Y') = '$year'   ◄■■■■
 ORDER BY contacts.siteid ASC

將先前的代碼復制並粘貼到名為“ report.html”和“ report.php”的兩個文件中,然后在瀏覽器中打開“ report.html”。

有許多要改進的地方,例如,使用jQuery,您可以改善用戶輸入月份和年份的方式,但這是另一個問題。

您可以使用HTML,可能使用javascript來訪問選定的月份和年份值並將其插入查詢模板的字符串中,以生成SQL查詢,但是如果要通過服務器端運行查詢,則可以不妨使用PHP或其他腳本語言。

Mashymoo,如果您使用Navicat,則可以使用Navicat Report Builder 希望這可以幫助。 經理們喜歡報道!

我認為最好的方法

將所有請求放入查詢(即稱為report_queries的sql表)中。 允許用戶根據需要每月,每天或每周請求數據。 使用Cron作業每隔2個小時左右檢查一次尚未生成的報告。 當然,如果仍然沒有生成任何報告,則僅生成其中的少數幾個(例如10個示例,以防止由於大量內存使用而導致服務器崩潰,並通過頁面中的通知或推送通知(Chrome瀏覽器)將生成的報告通知用戶,Android,iOS等)。 我個人使用的是FPDF庫,它是PHP PDF生成庫。 它允許使用html制作pdf。

這是幫助您形象化想法的圖:

用戶與腳本交互的關系圖

我編號了步驟:

  1. 用戶請求報告,在這里您檢查相同的記錄等。
  2. Cron工作發生,並調用腳本。
  3. 腳本檢查仍未生成的請求,並生成那些
  4. 腳本更新報告狀態並繼續。
  5. 腳本提供了報告文件以及該文件。

Yopu可以使用Windows的標准Navicat中包含的Navicat Report Builder來生成報告。

而且您不必每個月手動運行生成器,我們有一個名為Schedule的功能,您可以將報告生成任務添加到日程表中,報告將定期生成。

如有任何問題,請與我們聯系。

套件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM