How to Fetch All for Pdo Search

229 Views Asked by At

I am new to PHP, my PDO search seems to fetch single row from my database though it has five row data with the same date. How do i fetch all of the data that has the same date using PDO?

Below is my code.

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

Thanks in advance for your help!

2

There are 2 best solutions below

10
Sajjad Ali On

After execute statement you can use fetchAll method to get all the records.

Try the following code.

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';
    }
}
0
Stella On
 <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

are these what you need to see? Only the last value, "Arabian" shows up after search. "Boardroom" does not appear.