doushouj966020 2012-10-09 12:14
浏览 121
已采纳

PDO Select语句不返回任何内容(FETCH_ASSOC)

I'm trying to retrieve records from the mySQL-database but nothing gets selected. I'm using the below php function:

function printButton($conn){
$your_variable = 1;
$knappar = $conn->prepare('SELECT * FROM knappar WHERE pid  < :parameter');
$knappar->bindParam(':parameter', $your_variable, PDO::PARAM_INT);
$knappar->execute();

            //Loops boxes
          $count = 1;
          while ($row = $knappar->fetch(PDO::FETCH_ASSOC)) {
            echo "<a href='#'><div class='box' id='div_item".$count."'>";
            echo $row['header'];
            echo $row['id'];

            echo "</div></a>";
            $count = $count + 1;
            }

}

It works fine if I change "WHERE pid = 1" instead of the "< :parameter". What am I doing wrong?

I updated it to what it looks like now:

function printButton($conn){
$your_variable = 1;
$knappar = $conn->prepare('SELECT header FROM knappar WHERE pid < :parameter');
$knappar->bindParam(':parameter', $your_variable, PDO::PARAM_INT);
$knappar->execute();

            //Loops boxes
      $results = $knappar -> fetchAll(PDO::FETCH_ASSOC);
      foreach ($results as $row){

      echo $row['header'];
      echo '<br />';

      }

}

With above example, still no output. pid value is int(11) in the database and the value of all rows are 1.

  • 写回答

1条回答 默认 最新

  • doulu1867 2012-10-09 12:17
    关注

    $knappar -> fetch(PDO::FETCH_ASSOC) only fetches the next row of the result set. Use fetchAll to get the entire result set.

    You could then iterate through the returned array with a foreach.

    Granted, I'm not sure why it returns nothing rather than just the first row but give it a try.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建