简体   繁体   English

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

[英]PDO Select Statement returns nothing (FETCH_ASSOC)

I'm trying to retrieve records from the mySQL-database but nothing gets selected. 我正在尝试从mySQL数据库检索记录,但未选择任何内容。 I'm using the below php function: 我正在使用以下php函数:

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". 如果我更改“ WHERE pid = 1”而不是“ <: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. pid的值在数据库中为int(11),所有行的值均为1。

$knappar -> fetch(PDO::FETCH_ASSOC) only fetches the next row of the result set. $ knappar-> fetch(PDO :: FETCH_ASSOC)仅获取结果集的下一行。 Use fetchAll to get the entire result set. 使用fetchAll获取整个结果集。

You could then iterate through the returned array with a foreach. 然后,您可以使用foreach遍历返回的数组。

Granted, I'm not sure why it returns nothing rather than just the first row but give it a try. 当然,我不确定为什么它不返回任何内容,而只是返回第一行,而是尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM