douche5961 2019-02-02 09:55
浏览 99
已采纳

phpMyAdmin DATE变量到SELECT命令 - 致命错误:未捕获的ArgumentCountError

I'm new here and I learn PHP and SQL connection. I want to list from the SQL database the room numbers what are not reserved. I create 2 tables ( rooms and reservation ) with fill some exapmles.

Step 1

If I put direct DATE for the "end reservation" DATE("2019-03-11") like this work well and listed the room numbers

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;
}

Step 2

I want to replace the fixed DATE("2019-03-11") to a variable.

$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;
}

I have in Step 2 the Fatal error Fatal error: Uncaught ArgumentCountError: Too few arguments to function reserv3(), 0 passed in

Can you help me how can I put variable to this command ? Thank you

  • 写回答

1条回答 默认 最新

  • doujiao1814 2019-02-02 11:46
    关注

    Something is wrong in your code. PHP date() generates the date. If you want the current date write date ('Y-m-d') for date and date ('Y-m-d H: i: s)' for the date and time. In mysql if the field is the date type you do not have to use the mysql date() function to compare. Only if it is of the datetime type use the date() function to extract the 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
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用