简体   繁体   English

mySQL表上的PHP脚本:FROM_UNIXTIME和INTO OUTFILE问题

[英]PHP script on mySQL table : FROM_UNIXTIME and INTO OUTFILE issues

This is my first php script for a mysql table. 这是我第一个mysql表的php脚本。

mySQL table looks as below : mySQL表如下所示:

在此处输入图片说明

From that table i would like to generate a dynamic HTML table with the following requirements : 我想从该表生成具有以下要求的动态HTML表:

  • HTML table sorted ASC by NEXT_EVENT HTML表格按NEXT_EVENT排序为ASC
  • HTML table gets a new column showing NEXT_EVENT value as human readable datetime. HTML表获得一个新列,该列将NEXT_EVENT值显示为人类可读的日期时间。

Once the table is sorted i need to output ID and NEXT_EVENT values from the very first row to a TXT file, the 2 values will be comma separared. 表格排序后,我需要将第一行的ID和NEXT_EVENT值输出到TXT文件,这两个值将用逗号分隔。

I tryed to write a PHP script, but it get errors. 我试图编写一个PHP脚本,但是它得到了错误。

Sorting ASC looks working properly, but conversion with FROM UNIXTIME does not and file output won't work either. 排序ASC看起来工作正常,但是使用FROM UNIXTIME进行的转换不起作用,并且文件输出也不起作用。

mysql table's name is : 'webpilot'

$result = mysqli_query($conn, "SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");

$table = '<table>
            <tr>
                <th>ID</th>
                <th>NAME</th>
                <th>NEXT_EVENT</th>
            </tr>';

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        mysqli_query($conn, "SELECT FROM_UNIXTIME(".$row['NEXT_EVENT'].") as $datetime");               

        $table .= '<tr>
                    <td>'.$row['ID'].'</td>
                    <td>'.$row['NAME'].'</td>
                    <td>'.$row['NEXT_EVENT'].'</td>
                    <td>'.$datetime.'</td>
                   </tr>';
    }
} else {
    $table .= '<tr><td colspan="3">No date found</td></tr>';
}
$table .= '</table>';
echo $table;

$fileoutput = mysql_query($conn, "SELECT ID,NAME,NEXT_EVENT FROM webpilot INTO OUTFILE '/next_event.txt' LINES TERMINATED BY '\n'");

The live result can be seen here : http://s529471052.onlinehome.fr/bs3/gpio/dyntable.php 实时结果可以在这里看到: http : //s529471052.onlinehome.fr/bs3/gpio/dyntable.php

but i had to comment out the following line, as it reports an error avoiding the rest of script to be run and php webpage to be displayed. 但我不得不注释掉以下行,因为它报告了一个错误,以避免运行其余脚本并显示php网页。

mysqli_query($conn,"SELECT FROM_UNIXTIME(.$row['NEXT_EVENT']) as $datetime");

Line 203 is related to that line : 第203行与该行相关:

$fileoutput = mysql_query($conn,"SELECT ID,NAME,NEXT_EVENT FROM webpilot INTO OUTFILE '/next_event.txt' LINES TERMINATED BY '\n'"); 

Would you please show me how to get expected result. 您能告诉我如何获得预期的结果吗?

can you check mysql function is wrong mysql_query is there u know that one you need to change mysqli_query 您能检查mysql函数是否错误mysql_query吗?您知道需要更改mysqli_query吗?

$fileoutput = mysqli_query($conn,"SELECT ID,NAME,NEXT_EVENT FROM webpilot INTO `OUTFILE '/next_event.txt' LINES TERMINATED BY '\n'");`

and

$datetime you need to assign values. $ datetime您需要分配值。 Example like $datetime=date("Ymd H:i:s") $ datetime = date(“ Ymd H:i:s”)这样的示例

HTML table sorted ASC by NEXT_EVENT (You are correct) HTML表格按NEXT_EVENT排序为ASC(正确)

SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC

HTML table gets a new column showing NEXT_EVENT value as human readable datetime. HTML表获得一个新列,该列将NEXT_EVENT值显示为人类可读的日期时间。 (You can try to more easy way, do inside php) (您可以尝试更简单的方法,在php内做)

$getDateTime = mysqli_query($conn,"SELECT NEXT_EVENT FROM webpilot where NEXT_EVENT=".$row['NEXT_EVENT'].");

$rowDateTime = mysqli_fetch_array($getDateTime );

$datetime = date("Y-m-d H:i:s", $rowDateTime('NEXT_EVENT'));

MYSQL ways: MYSQL的方式:

SELECT ID, FAVORITE, NAME, ORDERTYPE, STATE, LAST_EVENT RECCURENCY, FROM_UNIXTIME(timestamp) as datetime FROM webpilot ORDER BY timestamp ASC

so your this code mysqli_query($conn, "SELECT FROM_UNIXTIME(".$row['NEXT_EVENT'].") as $datetime"); 所以您的这段代码mysqli_query($conn, "SELECT FROM_UNIXTIME(".$row['NEXT_EVENT'].") as $datetime"); can be remove. 可以删除。

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

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