简体   繁体   English

PHP中的SQL查询

[英]sql queries in php

I run a small helpdesk and I have a requirement to produce a ticket report every month to my line manager. 我经营着一个小型的服务台,我要求每个月向我的直属经理制作一份工单报告。

I am looking to upgrade the way in which we produce this report because at present, I run the below query I wrote manually every month by using Navicat: 我正在寻求升级生成此报告的方式,因为目前,我运行每月使用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

I would like to put this all in a very simple webpage allowing the user select the month and year and then offer them a HTML formatted download. 我想将所有内容放在一个非常简单的网页中,允许用户选择月份和年份,然后为他们提供HTML格式的下载。

Would I be best placed to put this in PHP or can I create it in HTML? 我最好放在PHP中还是可以用HTML创建? Please be kind, I am a total newbie and my thought process is a little off. 请客气,我是一个新手,我的思维过程还差一点。

Many thanks for your time and patience! 非常感谢您的耐心配合!

I'll show you the basic : first, one HTML file where the user enters month and year as numbers (notice the call to "report.php") : 我将向您展示基本的内容:首先,一个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>

Now the PHP file "report.php" (notice how month and year are captured at the beginning and stored in variables $month and $year ) : 现在,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.
?>

You have to replace the text "YOUR BIG SELECT HERE" by your big query. 您必须用大查询替换文本“ YOUR BIG SELECT HERE”。 This is how you insert variables $month and $year in the query : 这是在查询中插入变量$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

Copy-paste previous codes in two files named "report.html" and "report.php", then open "report.html" in your browser. 将先前的代码复制并粘贴到名为“ report.html”和“ report.php”的两个文件中,然后在浏览器中打开“ report.html”。

There are many things to improve, for example, with jQuery you can improve how the user enters month and year, but that's for another question. 有许多要改进的地方,例如,使用jQuery,您可以改善用户输入月份和年份的方式,但这是另一个问题。

您可以使用HTML,可能使用javascript来访问选定的月份和年份值并将其插入查询模板的字符串中,以生成SQL查询,但是如果要通过服务器端运行查询,则可以不妨使用PHP或其他脚本语言。

Mashymoo, if you use Navicat, you could use Navicat Report Builder . Mashymoo,如果您使用Navicat,则可以使用Navicat Report Builder Hope this helps. 希望这可以帮助。 Managers love reports! 经理们喜欢报道!

Best way to do it in my opinion 我认为最好的方法

Place all requests in query (ie sql table called report_queries). 将所有请求放入查询(即称为report_queries的sql表)中。 Allow users to request data every month, day or week as they need. 允许用户根据需要每月,每天或每周请求数据。 Use Cron jobs to check every 2 hours or so for not yet generated reports. 使用Cron作业每隔2个小时左右检查一次尚未生成的报告。 Of course if there any reports that is still not generated, generate only few of them, 10 in example to prevent server crash due large memory usage, and inform user about generated report either via notification in your page, or something like push notifications (Chrome, Android, iOS etc). 当然,如果仍然没有生成任何报告,则仅生成其中的少数几个(例如10个示例,以防止由于大量内存使用而导致服务器崩溃,并通过页面中的通知或推送通知(Chrome浏览器)将生成的报告通知用户,Android,iOS等)。 I personally am using FPDF library that is PHP PDF generation library. 我个人使用的是FPDF库,它是PHP PDF生成库。 It allows using html to make pdf. 它允许使用html制作pdf。

Here is a diagram to help you visualize the idea: 这是帮助您形象化想法的图:

用户与脚本交互的关系图

I numbered the steps : 我编号了步骤:

  1. User request for report, here you check for same records etc. 用户请求报告,在这里您检查相同的记录等。
  2. Cron job takes place, and calls script. Cron工作发生,并调用脚本。
  3. Script checks for requests that still have not been generated and generates those 脚本检查仍未生成的请求,并生成那些
  4. Script update report status and continues. 脚本更新报告状态并继续。
  5. Script provides report file and that it. 脚本提供了报告文件以及该文件。

Yopu can use Navicat Report Builder included in standard Navicat for Windows to generate a report. Yopu可以使用Windows的标准Navicat中包含的Navicat Report Builder来生成报告。

And you don't have to run the generator manually every month, We have a feature named Schedule , you can add the report generation task to a schedule and the report will be generated regularly. 而且您不必每个月手动运行生成器,我们有一个名为Schedule的功能,您可以将报告生成任务添加到日程表中,报告将定期生成。

Feel you to contact us if you have any problem. 如有任何问题,请与我们联系。

Kit. 套件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM