【问题标题】:How to Fetch All for Pdo Search如何获取所有 Pdo 搜索
【发布时间】:2019-05-01 03:36:20
【问题描述】:

我是 PHP 新手,我的 PDO 搜索似乎从我的数据库中获取单行,尽管它有五个具有相同日期的行数据。如何使用 PDO 获取所有具有相同日期的数据?

下面是我的代码。

if(isset($_POST['Find']))
{
        // connect to mysql
    try {
        $pdoConnect = new PDO("mysql:host=localhost;dbname=testing","root","");
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }

    // id to search
    $check_date = $_POST['check_date'];

     // mysql search query
    $pdoQuery = "SELECT * FROM checklist WHERE check_date = :check_date";

    $pdoResult = $pdoConnect->prepare($pdoQuery);

    //set your id to the query id
    $pdoExec = $pdoResult->execute(array(":check_date"=>$check_date));

    if($pdoExec)
    {
            // if id exist 
            // show data in inputs
        if($pdoResult->rowCount()>0)
        {
            foreach($pdoResult as $row)
            {
                $check_date = $row['check_date'];
            }
        }
            // if the id not exist
            // show a message and clear inputs
        else{
            echo 'No Data With This ID';
        }
    }else{
        echo 'ERROR Data Not Inserted';
    }
}

提前感谢您的帮助!

【问题讨论】:

    标签: search pdo fetchall


    【解决方案1】:
     <form action="" method="post">
    
                Date : <input type="text" name="check_date" value="<?php echo $check_date;?>"><i><font color="#a30017">&nbsp;&nbsp;*format : dd / mm / yy | *eg : 29 / 11 / 18 </font></i><br><br>
    
                <input type="submit" name="Find" value="Search">
    
            </form> 
    
    
                  <table>
              <tr>
                <td>No</td>
                <td>Room</td>
                <td>Projector</td>
                <td>Check Date</td>
    
              </tr>
                <td><?php echo $count++; ?></td>
                <td><?php echo $row['meeting_room']; ?></td>
                <td style="text-align: center; vertical-align: middle;"><?php echo $row['projector']; ?></td>
                <td style="text-align: center; vertical-align: middle;"><?php echo $row['check_date']; ?></td>
    
                </tr>
    
            </table>
    

    this is my database

    这些是您需要看到的吗?搜索后仅显示最后一个值“Arabian”。 “会议室”没有出现。

    【讨论】:

      【解决方案2】:

      执行语句后可以使用fetchAll方法获取所有记录。

      试试下面的代码。

      if(isset($_POST['Find']))
      {
              // connect to mysql
          try {
              $pdoConnect = new PDO("mysql:host=localhost;dbname=testing","root","");
          } catch (PDOException $exc) {
              echo $exc->getMessage();
              exit();
          }
      
          $check_date = $_POST['check_date']."%";
      
          $pdoQuery = "SELECT * FROM checklist WHERE check_date like :check_date";
      
          $pdoResult = $pdoConnect->prepare($pdoQuery);
      
          $pdoExec = $pdoResult->execute(array(":check_date"=>$check_date));
      
          if($pdoExec)
          {
              if($data=$pdoResult->fetchAll(PDO::FETCH_ASSOC))//new added lines
              {
                  foreach($data as $row)
                  {
                      echo $row['check_date']."<br>";
                  }
      
              }else{  
                  echo "Data not found";
              }//end new added lines
          }else{
              echo 'ERROR Data Not Inserted';
          }
      }
      

      【讨论】:

      • 非常感谢您提供的代码!试过,但它不起作用。它仍然在搜索的特定日期的数据库中显示一个和最后一个值。它不会获取具有相同日期的整行。
      • 能否请您回显您的“$check_date”变量并检查您得到了什么
      • 您能告诉我您的表格中的“check_date”格式吗?只是为了比较你得到的东西和你桌子上的东西。
      • 您好,我对您提出的问题发表了新评论。不确定给出的内容是您需要查看的内容。
      • 实际上我想查看保存在“清单”表中的 check_date 值以及您从 $_POST['check_date'] 获得的值
      猜你喜欢
      • 2023-02-26
      • 1970-01-01
      • 2011-06-26
      • 2018-03-24
      • 2015-03-20
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多