1

I have been doing this for quite a few queries but suddenly found one where it is not working. Here is my code:

$q = 'SELECT title FROM blog LIMIT :paging,:perpage';
$v = array(
    ':paging'=>(($page-1)*$perpage),
    ':perpage'=>$perpage
);
$sql = $conn->prepare($q);
$sql->execute($v);
if ($sql){
    foreach($sql as $rs){
        $title = $rs['title'];
        echo '<article>'.$title.'</article>';
    };
};

I am not getting any errors however It is also not displaying anything. When I take out $v and just put the values into the query like so:

$q = 'SELECT title FROM blog LIMIT '.(($page-1)*$perpage).','.$perpage.' ';

This works perfectly and outputs fields.

I have does this same array trick on many other queries in the past and never had any problems. Not sure what I did wrong here, need a fresh pair of eyes.

also $page = 1 and $perpage = 2

3 Answers 3

4

Its because when you use an array for execute, it defaults to PDO::PARAM_STR, so it thinks its a string...which obviously doesn't play well with LIMIT

Instead use bindValue, that way you can explicitly state PDO::PARAM_INT

 $q = 'SELECT title FROM blog LIMIT :paging,:perpage';
 $sql = $conn->prepare($q);

 $sql->bindValue(':paging',(($page-1)*$perpage), PDO::PARAM_INT);
 $sql->bindValue(':perpage',$perpage, PDO::PARAM_INT);
 $sql->execute();
Sign up to request clarification or add additional context in comments.

7 Comments

@MihaiStancu Your wrong, this answer is correct, execute() An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.
They are not treated as string (nor would it matter if they are). They type is inspected and they are treated according to type.
Thanks bindParam almost worked, changed it to bindValue and that came through for me. Have to wait for time limit to accept this as the answer
@MihaiStancu from your link - public bool PDOStatement::execute ([ array $input_parameters ] ) :: input_parameters :: An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR. All values are treated as strings
Its not that yours worked or mine worked or whatever...its that I provided solid reasoning behind why his wasnt working, and some code...you didn't :) Effort pays off sometimes
|
3

You need to use bindValues to bund the values to the query the use execute with no parameters!

http://php.net/manual/en/pdostatement.bindvalue.php

7 Comments

Execute is meant to allow you to specify all bindvalues in one array; php.net/manual/ro/pdostatement.execute.php; OP's code looks correct at first glance.
@MihaiStancu he didn't say you can't use an array - he just said that the OP forgot to bind the params
OP didn't forget... he used ->execute($v)
@MihaiStancu he neither used bind nor sent the array to execute
+1 as technicality correct, though a simple example will encourage people to upvote.
|
-1

You're not getting any PHP errors but you're not checking for SQL errors at all. You need to explicitly check for SQL errors.

try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
} 

2 Comments

downvoting without commenting (or as a childish "revenge") is lame!
My answer directs OP to investigate his SQL error and see for himself what the problem is. It's not an answer per se so I have no problem with their downvotes because it doesn't provide a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.