0

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!

Stella
  • 1

2 Answers2

0

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';
    }
}
Sajjad Ali
  • 304
  • 3
  • 12
  • Thank you very much for the code! Tried but it doesnt work. It still displays one and last value in database of specific date searched. It wont fetch the entire row that have the same date. – Stella Nov 29 '18 at 08:27
  • Could you please echo out your "$check_date" variable and check what you are getting – Sajjad Ali Nov 29 '18 at 08:37
  • and could you please show me the "check_date" format that you have in your table? Just to compare what you are getting and what you have in your table. – Sajjad Ali Nov 29 '18 at 08:38
  • hi i have made a new comment for what you have asked. Not sure whats was given is what you needed to see. – Stella Nov 30 '18 at 02:20
  • actually i want to see the check_date value that is saved in your "checklist " table and the value that you are getting from $_POST['check_date'] – Sajjad Ali Nov 30 '18 at 06:20
  • the check_date value in my table i put as "varchar", and the $_POST['check_date'] value is as – Stella Nov 30 '18 at 06:48
  • Sorry if i couldnt give what you actually ask for but i'd definitely look for it. – Stella Nov 30 '18 at 06:49
  • OK, I believe that your client table has "check_date" in "d/m/y" format. could you update your Query using like keyword in where condition. I have updated my answer can you try updated code. – Sajjad Ali Nov 30 '18 at 07:05
  • Yes it is in d/m/y format. Thank you for the code. i've tried your updated code and it still stays the same. Only one last value was pulled out. – Stella Nov 30 '18 at 07:11
  • Its okay i appreciate your help and all of your solutions! – Stella Nov 30 '18 at 07:33
0
 <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.

Stella
  • 1