1

I have specific issue:

if (isset($_POST['dateselected'])) {
                $selecteddate = $_POST['dateselected'];
                list($chosendate) = explode(' ', trim($selecteddate));
                $showdata = $db->prepare("SELECT * FROM adherence WHERE dateandtime= :chosendate AND lastname= :lastname");
                $showdata->bindParam(':chosendate', $chosendate);
                $showdata->bindParam(':lastname', $lastname);
                $showdata->execute();
                foreach ($showdata as $rowas){
                echo '<div class="twenty name">', $rowas["lastname"], $rowas["firstname"], '</div>';
}}

Here is my code, how to print out something, if there is no such result as requested in script? Lets say Database does not contain this kind of condition as it given in: WHERE dateandtime= :chosendate AND lastname= :lastnameHow to check returned values, and print out something if there are no correct result coming back?

JustinasT
  • 561
  • 1
  • 8
  • 27
  • I'm using rowCount() after $showdata->execute(); Value it ALLWAYS get is: -1 – JustinasT Dec 03 '15 at 21:39
  • Hmm, I thought I'd used that in the past for `selects` but I guess not doc actually says `For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement.`. So guess that was a poor comment, http://stackoverflow.com/questions/6041886/how-to-get-the-number-of-rows-grouped-by-column. – chris85 Dec 03 '15 at 21:46

2 Answers2

2

Try following:

if (isset($_POST['dateselected'])) {
            $selecteddate = $_POST['dateselected'];
            list($chosendate) = explode(' ', trim($selecteddate));
            $showdata = $db->prepare("SELECT * FROM adherence WHERE dateandtime= :chosendate AND lastname= :lastname");
            $showdata->bindParam(':chosendate', $chosendate);
            $showdata->bindParam(':lastname', $lastname);
            $showdata->execute();
            if ($rowas= $showdata->fetch(PDO::FETCH_ASSOC)) {
                do {
                    //something with data
                    echo '<div class="twenty name">', $rowas["lastname"], $rowas["firstname"], '</div>';
                } while ($rowas= $showdata->fetch());
            } else {
                echo 'no data';
            }
thepiyush13
  • 1,321
  • 1
  • 8
  • 9
0

chris 85 mentioned rowcount and this is the right way. This avoid the fetch process if nothing exist. fastest method, it s made for. Also, to speed up the preprocess binding, it is better to specify what data type it is (PDO::PARAM_STR).

if (isset($_POST['dateselected'])) {
            $selecteddate = $_POST['dateselected'];
            list($chosendate) = explode(' ', trim($selecteddate));
            $showdata = $db->prepare("SELECT * FROM adherence WHERE dateandtime= :chosendate AND lastname= :lastname");
            $showdata->bindParam(':chosendate', $chosendate,PDO::PARAM_STR);
            $showdata->bindParam(':lastname', $lastname,PDO::PARAM_STR);
            $showdata->execute();
            $count= $showdata->rowCount();
            if ($count !==0){
                $rowas= $showdata->fetch(PDO::FETCH_ASSOC)
                foreach ($showdata as $rowas){
                     echo '<div class="twenty name">', $rowas["lastname"], $rowas["firstname"], '</div>';
                }}
            }else {
            echo 'no data';
            }
cpugourou
  • 775
  • 7
  • 11