繁体   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