PHP PDO bindValue() weird problem

Posted by TheMagician on Stack Overflow See other posts from Stack Overflow or by TheMagician
Published on 2010-04-24T18:04:01Z Indexed on 2010/04/24 18:43 UTC
Read the original article Hit count: 451

Filed under:
|
<?php
try
{
    $db = new PDO("mysql:host=localhost;dbname=DBNAME", "USER", "PASSWD");

    $stmt = $db->prepare("SELECT id, name FROM testdb ORDER BY time DESC LIMIT :index, 10");
    $stmt->bindValue(":index", $_GET['index'], PDO::PARAM_INT);
    $stmt->execute();

    while( $r = $stmt->fetch(PDO::FETCH_ASSOC) )
    {
        echo var_dump($r);                                      
    }

}
catch( PDOException $e )
{
    die("Exception");
}

The problem is on this line: $stmt->bindValue(":index", $_GET['index'], PDO::PARAM_INT);

And the specific parameter is the second one.

The code as it is above doesn't work, it doesn't return anything so the while loop isn't executed. If I replace $_GET['index'] with a number, like 10, it works just fine, it returns 10 rows. Echoing $_GET['index'] displays a number, so it should pass a number. I've also tried bindParam, but the result is same.

Why isn't this working?

© Stack Overflow or respective owner

Related posts about php

Related posts about pdo