2

我在 MySQL 中有一个查询(感谢用户Ross Smith II),我在控制台中测试过,(几小时前返回 1 天 10 小时 34 分钟)我相对确定我的 PHP 是正确的,尽管我可能是忽略某些东西,或者这个查询的某个地方有一个条件,我不能做我想做的事情。真的很感激一些见解。请注意,顶部的时间戳将是从另一个表中选择的变量,但出于此查询的目的,我以相同的格式设置了时间。我已经取出所有变量并尝试了原始数据,仍然可以在 SQL 查询中使用,但我无法让 PHP 回显它。我收到致命错误:调用非对象上的成员函数 fetch_assoc()。再次感谢。

$aquery = ("SET @start = \"2013-01-19 07:56:22\";
        SET @end = NOW();

        SELECT
        CONCAT(
            FLOOR(
                (
                    UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                        (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                            IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                            IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
                    )
                ) / 86400
            ),
            ' days ',
            TIME_FORMAT(
            SEC_TO_TIME(
                (
                    UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                        (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                            IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                            IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
                    )
                ) % 86400
            ),
            '%H hours %i minutes'
            )
        ) AS duration");
        $aresult = $mysqli->query($aquery);
        // while( $row = $result->fetch_assoc() ) {
        // Edited typo above, does not fix.
        while ( $row = $aresult->fetch_assoc() ) {
        echo ($row['duration']);
        }
4

4 回答 4

2
$aresult = $mysqli->query($aquery);
while( $row = $result->fetch_assoc() ) {
        echo ($row['duration']);
}

这看起来像一个错字。你不应该使用$aresult->fetch_assoc()吗?

于 2013-01-23T03:03:43.633 回答
0

您不能像这样使用多个查询来调用查询。单独查询它们中的每一个(并删除 ;'s)或使用multi_query

$aresult = $mysqli->multi_query($aquery);

或者

$mysqli->query("SET @start = \"2013-01-19 07:56:22\"");
$mysqli->query("SET @end = NOW()");
$result = $mysqli->query("SELECT
    CONCAT(
        FLOOR(
            (
                UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                    (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                        IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                        IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
                )
            ) / 86400
        ),
        ' days ',
        TIME_FORMAT(
        SEC_TO_TIME(
            (
                UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                    (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                        IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                        IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
                )
            ) % 86400
        ),
        '%H hours %i minutes'
        )
    ) AS duration");
于 2013-01-23T03:23:03.777 回答
0

您有多个查询,不能像您尝试的方式那样直接执行。我正在使用 PDO,所以我不确定这是否会满足您的要求。但是试一试。

$mysqli->autocommit(FALSE);
$mysqli->query("SET @start = '2013-01-19 07:56:22'");
$mysqli->query("SET @end = NOW()");
/*$aresult = */$mysqli->query("SELECT
    CONCAT(
        FLOOR(
            (
                UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                    (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                        IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                        IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
                )
            ) / 86400
        ),
        ' days ',
        TIME_FORMAT(
        SEC_TO_TIME(
            (
                UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                    (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                        IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                        IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
                )
            ) % 86400
        ),
        '%H hours %i minutes'
        )
    ) AS duration");
$mysqli->commit();
于 2013-01-23T03:26:58.707 回答
0

在研究了 mysqli_multi_query 并考虑了inhanring0 的答案之后,感谢Ross Smith II,这是我想出的解决方案。如果有人需要 PHP 脚本从时间戳(此处为 $timeApproved)和当前时间(可以替换为任何其他时间戳,将 @end = NOW() 替换为您的时间戳...)计算经过的时间,这里就是。它将以 0 天 0 小时 0 分钟的格式返​​回时间量。非常感谢帮忙。

这是更新的代码

$link = mysqli_connect("localhost", "username", "password", "database");
$query = "SET @start = \"".$timeApproved."\";"; 
$query .= "SET @end = NOW();";
$query .= "SELECT
CONCAT(
    FLOOR(
        (
            UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                    IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                    IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
            )
        ) / 86400
    ),
    ' days ',
    TIME_FORMAT(
    SEC_TO_TIME(
        (
            UNIX_TIMESTAMP(@end) - UNIX_TIMESTAMP(@start) - 86400 * (
                (FLOOR(DATEDIFF(@end, @start) / 7) * 2) +
                    IF(WEEKDAY(@end) > 4, WEEKDAY(@end) - 4, 0) +
                    IF(WEEKDAY(@end) < WEEKDAY(@start), 2, 0)
            )
        ) % 86400
    ),
    '%H hours %i minutes'
    )
) AS duration;"; 

if (mysqli_multi_query($link, $query)) {
do {

if ($result = mysqli_store_result($link)) {
    while ($row = mysqli_fetch_array($result)) 

{
echo $row['duration'];
}

// mysqli_free_result($result); // I will only have one result.
}   
} while (mysqli_next_result($link));
}
于 2013-01-23T04:33:12.803 回答