while循环重复mysql表中数组的最后一行

iugsix8n  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(325)

我想从我的mysql表中获取所有行,该表与我的查询相匹配,现在我有两行与匹配的查询,我正在保存其中的每一行 $tourData 变量,而不是添加到 $allTourData while循环中的变量。
此函数返回我的 $allTourData 但我不是第一排和第二排,而是第二排两次。
这是我的密码。

public static function getTourAvailabilityStatus($date, $route)
{
    $lt   = MySQL::connect();
    $tour = $lt->prepare
    (   /**@lang MySQL */
        "
            SELECT id, token, route, date, travels_name, bus_number, st, aj, wt, sl
            FROM tour
            WHERE date = ? AND route = ? ;
            "
        );

    MySQL::error($tour, $lt);
    $tour->bind_param('ss', $date, $route);
    MySQL::error($tour, $lt);
    $tour->execute();
    MySQL::error($tour, $lt);
    $tourData = [];
    $tour->bind_result(
        $tourData['id'], $tourData['token'], $tourData['route'], $tourData['date'], $tourData['travelsName'],
        $tourData['BusNumber'], $tourData['st'], $tourData['aj'], $tourData['wt'], $tourData['sl']
    );
    MySQL::error($tour, $lt);
    $allTourData = [];
    while($tour->fetch())
    {
        $allTourData[] = $tourData;
    }
    MySQL::error($tour, $lt);
    $tour->close();

    return $allTourData;
}
xpcnnkqh

xpcnnkqh1#

这应该可以做到:

public static function getTourAvailabilityStatus($date, $route)
{
    $lt   = MySQL::connect();
    $tour = $lt->prepare
    (   /**@lang MySQL */
        "
            SELECT id, token, route, date, travels_name, bus_number, st, aj, wt, sl
            FROM tour
            WHERE date = ? AND route = ? ;
            "
        );

    MySQL::error($tour, $lt);
    $tour->bind_param('ss', $date, $route);
    MySQL::error($tour, $lt);
    $tour->execute();
    MySQL::error($tour, $lt);

    $tour->bind_result(
        $id, $token, $route, $date, $travelsName,$BusNumber, $st, $aj, $wt, $sl
    );
    MySQL::error($tour, $lt);
    $allTourData = [];
    while($tour->fetch())
    {
        $allTourData[] = [ $id, $token, $route, $date, $travelsName,$BusNumber, $st, $aj, $wt, $sl];
    }
    MySQL::error($tour, $lt);
    $tour->close();

    return $allTourData;
}

相关问题