0

i try get data from db with pdo but returns error [42000].

if(isset($_POST['number'])){
    $start = $_POST['number'];
}else{
    $start = 8;
}
if(isset($_POST['page'])){
    $page = $_POST['page'];
}else{
    $page = 1;
}
$end = 4;


$db = new Database();

$data = $db->connect()->prepare('SELECT img FROM gallery_menu WHERE gallery_id = :page LIMIT `:start_page` , :end_page');
$data->bindValue(':page', $page);
$data->bindValue(':start_page', $start);
$data->bindValue(':end_page', $end);

$data->execute();
print_r($data->errorInfo());

$images = $data->fetchAll(7);

and error is:

Array ( [0] => 42000 [1] => 1327 [2] => Undeclared variable: '4' ) Array ( [0] => 42000 [1] => 1327 [2] => Undeclared variable: '8' )

Dee_wab
  • 1,171
  • 1
  • 10
  • 23
  • Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''4' , '4'' at line 1 ) – გენო მუმლაძე Jun 06 '18 at 08:10
  • `LIMIT` needs to be an integer. You can't put a variable like this nor use [execute](https://secure.php.net/manual/en/pdostatement.execute.php). If you want to use a variable in your LIMIT close you need to use [bindParam](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Anthony Jun 06 '18 at 08:23

1 Answers1

0
$data = $db->connect()->prepare('SELECT img 
                                FROM gallery_menu 
                                WHERE gallery_id = :page 
                                LIMIT :start_page , :end_page');
Sersh
  • 308
  • 1
  • 11
  • new error: Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''4' , '4'' at line 1 ) – გენო მუმლაძე Jun 06 '18 at 08:09
  • **Good answers** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Jun 06 '18 at 08:10
  • edited his answer – Sersh Jun 06 '18 at 08:10