SELECT命令的phpMyAdmin DATE变量-致命错误:Uncaught ArgumentCountError

时间:2019-02-02 09:55:10

标签: php sql

我是这里的新手,我学习PHP和SQL连接。我想从SQL数据库中列出未保留的房间号。我创建了2个表(房间和预订),并填充了一些示例。

步骤1

如果我在这样的“最终预订” DATE(“ 2019-03-11”)中直接输入日期,并列出房间号

function reserv3()
{
    include "config.php";
    $html = "";
    $sql = 'SELECT roomnr , reserv_start FROM rooms LEFT OUTER JOIN reservation ON reservation.room_id = rooms.room_id AND DATE(reservation.reserv_start)  >= DATE("2019-03-11")';
    $result = mysqli_query($conn, $sql);
    while( $sor = mysqli_fetch_array($result) )
        $html .= "<DIV>".$sor['roomnr'].", ".$sor['reserv_start']."</DIV>";
    return $html;
}

步骤2

我想将固定的DATE(“ 2019-03-11”)替换为变量。

$today = DATE("2019-03-11");
function reserv3($today)
{
    include "config.php";
    $html = "";
    $sql = 'SELECT roomnr , reserv_start FROM rooms LEFT OUTER JOIN reservation ON reservation.room_id = rooms.room_id AND DATE(reservation.reserv_start)  >= '.$today;     
    $result = mysqli_query($conn, $sql);
    while( $sor = mysqli_fetch_array($result) )
        $html .= "<DIV>".$sor['roomnr'].", ".$sor['reserv_start']."</DIV>";
    return $html;
}

在第2步中出现致命错误 致命错误:未捕获的ArgumentCountError:函数reserv3()的参数太少,传入了0

您能帮我怎样将变量放入此命令? 谢谢

1 个答案:

答案 0 :(得分:0)

您的代码中有错误。 PHP date()生成日期。如果要当前日期写 日期('Y-m-d')表示日期和时间('Y-m-d H:i:s)'表示日期和时间。 在mysql中,如果字段是日期类型,则不必使用mysql date()函数进行比较。仅当它是datetime类型时,才使用date()函数提取日期。

<?php
include "config.php";

$today = DATE('Y-m-d');

function reserv3($today)
{
    global $conn;

    $sql = 'SELECT t1.roomnr , t2.reserv_end FROM reservation as t2 LEFT JOIN rooms as t1 ON t2.room_id = 
               t1.room_id where t2.reserv_end <= '.$today;     
    $result = mysqli_query($conn, $sql);

    if(mysqli_num_rows($result)>0){
     // do something
    }

}