dqch34769 2018-01-21 19:21
浏览 132
已采纳

PDO fetchAll命令中未返回值

I want to retrieve first four rows, the second grooup of four rows and so on until the end of the MySQL table named Productos. If I make queries from PHPMyAdmin values are retrieved correctly. But when I try to retrieve them from PHP no values are retrieved.

The code:

$this -> minimo = $_GET["minimo"];
echo "</br>" . $this -> minimo;

$this -> qry = "SELECT nombre,descripcion,precio,foto FROM productos       
LIMIT ?,4";
$this -> sqry = $this -> conexion -> prepare($this -> qry);
$this->sqry->bindParam(1,$this -> minimo,PDO::PARAM_INT);
$this -> sqry -> execute();

echo "</br>"; var_dump($this -> sqry);

$this -> resul = $this -> sqry -> fetchAll();

echo "Resul es " ; var_dump($this -> resul);

After running the script I watch the following:

0

object(PDOStatement)#4 (1) { 
        ["queryString"]=> string(62) "SELECT nombre,descripcion,precio,foto FROM productos LIMIT ?,4" }

Resul es array(0) { } 

How can I retrive values?

Thanks

  • 写回答

1条回答 默认 最新

  • doumo2501 2018-01-21 19:34
    关注

    Because you are using lazy loading i.e. an array in the execute parameter

    $this -> sqry -> execute(array($this -> minimo));
    

    All parameters are treated as strings, which will generate

    LIMIT '1', 4
    

    And '1' is illegal syntax

    So you have to specifiy the type like this

    $this->minimo = $_GET["minimo"];
    $this->qry = "SELECT nombre,descripcion,precio,foto FROM productos LIMIT ?,4";
    $this->sqry = $this->conexion->prepare($this->qry);
    $this->sqry->bindParam(1, $this->minimo, PDO::PARAM_INT);
    $this->sqry->execute();
    $this->resul = $this->sqry->fetchAll();
    
    // now $this->resul should be an array of arrays unless 
    // you have set PDO up to return Object by default
    // so a simple print_r() should show you whats in the returned data
    print_r($this->resul);
    

    Alternatively you can turn off Emulated Prepares as part of your connection and continue using the Lazy Load method

    $this->conexion->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    
    $this->minimo = $_GET["minimo"];
    $this->qry = "SELECT nombre,descripcion,precio,foto FROM productos LIMIT ?,4";
    $this->sqry = $this -> conexion -> prepare($this -> qry);
    $this->sqry->execute(array($this -> minimo));
    
    $this->resul = $this -> sqry -> fetchAll();
    

    If you just want the first 4 rows you could use LIMIT 4 but it would probably be a good idea to add an ORDER BY to make sure you get the correct first 4 rows maybe like this LIMIT 4 ORDER BY id

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?