-1

I have a problem with SQL syntax. NON working code:

$showdata = $db->prepare("SELECT * FROM adherence WHERE dateandtime='$chosendate' AND lastname='$lastname'");

Working code 1:

$showdata = $db->prepare("SELECT * FROM adherence WHERE dateandtime='$chosendate'");

Working code 2:

$showdata = $db->prepare("SELECT * FROM adherence WHERE lastname='$lastname'");

Separately - code works, but when using AND I get no results from database. Any idea?

EDIT

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

EDIT 2 DATABASE: enter image description here

ECHO'ing $chosendate and $lastname just above script. Result:

enter image description here

JustinasT
  • 561
  • 1
  • 8
  • 27

1 Answers1

1

I think you've made a mistake in logic of your query, maybe there isn't any row which satisfies these two conditions. Apart from that you should use Prepared Statement properly:

$showdata = $db->prepare("SELECT * FROM adherence WHERE
dateandtime= :chosendate AND lastname= :lastname");
$showdata->bindParam(':chosendate', $chosendate);
$showdata->bindParam(':lastname', $lastname);
$showdata->execute();