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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号