-3

Here is my code

$sql3= "select * 
        from comments 
        where status=:status  
        limit=:limit 
        offset=:offset 
        order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status",'n');
$query3->bindValue(":limit",$per_page);
$query3->bindValue(":offest",$offset);
$query3->execute();
$comments=$query3->fetchall();

Here comments is my table name status and time is two column in my table . Whenever I run this code , It shows a warning

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in E:\XAMPP\htdocs\parlament\user\logged_in_area.php

What does this mean ?

zessx
  • 68,042
  • 28
  • 135
  • 158
user3682521
  • 111
  • 1
  • 1
  • 4

6 Answers6

2

The answer depends of what are limit and offset.

If they're columns names...

  • You can't use these reserved keywords for column names without backticks
  • You need to add a AND / OR operator between your lines
$sql3= "select * 
        from comments 
        where status=:status  
        and `limit`=:limit 
        and `offset`=:offset 
        order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status", 'n');
$query3->bindValue(":limit", $per_page);
$query3->bindValue(":offest", $offset);
$query3->execute();
$comments=$query3->fetchall();

If they're keywords...

  • The syntax is LIMIT <n>, not LIMIT = <n> (same for OFFSET)
  • It better to specify their type with PDO::PARAM_INT (same for OFFSET)
  • ORDER BY must be added before LIMIT and OFFSET
$sql3= "select * 
        from comments 
        where status=:status 
        order by time desc 
        limit :limit 
        offset :offset";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status", 'n');
$query3->bindValue(":limit", (int)$per_page, PDO::PARAM_INT);
$query3->bindValue(":offset", (int)$offset, PDO::PARAM_INT);
$query3->execute();
$comments=$query3->fetchall();
zessx
  • 68,042
  • 28
  • 135
  • 158
1

Adding my answer because nobody has mentioned this specific part yet...

MySQL is very picky about the data type of LIMIT parameters. You pretty much need to use bindParam(':limit', $per_page, PDO::PARAM_INT). I assume the same for OFFSET.

So, in summary

// because E_WARNING level errors are insufficient
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('SELECT * FROM `comments` WHERE `status` = :status ORDER BY `time` DESC LIMIT :limit OFFSET :offset');
$stmt->bindValue(':status', 'n');
$stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT); // spelt "offset"
$stmt->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
Phil
  • 157,677
  • 23
  • 242
  • 245
0

Try this->

$sql3= "select * 
        from comments 
        where status = ?  
        limit ?
        offset ? 
        order by time desc";
$query3= $pdo->prepare($sql3);
$query3->execute(array('n',$per_page,$offset));
$comments=$query3->fetchall();
Jitendra Yadav
  • 896
  • 1
  • 6
  • 14
-1

Not sure about that but I think that PDO::bindValue / bindParam works with variable references. You cannot set a static value as parameter. Try to replace

$query3->bindValue(":status",'n'); 

by

$n_value = 'n';
$query3->bindValue(":status",$n_value);

You also forgot the "AND" Keyword between your conditions

Steven
  • 313
  • 3
  • 10
-2

You're missing the AND or OR keyword in statement. Also, LIMIT is a reserved keyword, you'll need to backtick it or rename it to something else if you don't want to do that.

MisterBla
  • 2,355
  • 1
  • 18
  • 29
-4
$sql3= "select * 
        from comments 
        where status = ?  
        limit= ?
        offset= ? 
        order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(1,'n');
$query3->bindValue(2,$per_page);
$query3->bindValue(3,$offset);
$query3->execute();
$comments=$query3->fetchall();
zessx
  • 68,042
  • 28
  • 135
  • 158
user3460377
  • 13
  • 1
  • 7
  • 1
    You're treating limit as just another column inside the table. whilst you probably meant that it's the `LIMIT` keyword meant to limit results. – Mave May 28 '14 at 07:19