douanrang4728 2015-12-03 18:31
浏览 57
已采纳

SQL WHERE条件

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

  • 写回答

1条回答 默认 最新

  • douzhang1364 2015-12-03 18:39
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画