{ $strona = (int) $strona; $strona = $strona - 1; if($strona != 0) $strona = $strona * $limit; $query = DB::prepare('SELECT * FROM posty LIMIT :strona, :limit'); $query->bindValue(':strona', $strona, PDO::PARAM_INT); $query->bindValue(':limit', $limit, PDO::PARAM_INT); $query->execute(); return $result = $query->fetchAll(); }
to wszystko jest ok, ale gdy chce sobie zrobic taką paginacje "uniwersalną" czyli chce podstawic zmienna $from = 'posty':
{ $strona = (int) $strona; $strona = $strona - 1; if($strona != 0) $strona = $strona * $limit; $query = DB::prepare('SELECT * FROM :from LIMIT :strona, :limit'); $query->bindValue(':from', $from, PDO::PARAM_STR); $query->bindValue(':strona', $strona, PDO::PARAM_INT); $query->bindValue(':limit', $limit, PDO::PARAM_INT); $query->execute(); return $result = $query->fetchAll(); }
to dostaję oczywiscie takowy komunikat:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''posty' LIMIT 0, 4' at line 1' in C:\komputery\library\Pagination.php:16 Stack trace: #0 C:\komputery\library\Pagination.php(16): PDOStatement->execute() #1 C:\komputery\controller\IndexController.php(5): Pagination::paginate('1', 4) #2 C:\komputery\bootstrap.php(97): IndexController->index('1') #3 C:\komputery\public\index.php(5): require_once('C:\komputery\bo...') #4 {main} thrown in C:\komputery\library\Pagination.php on line 16
Gdy próbuję to zrobić z bindParam to jest to samo... nie mam juz pojecia co jest nie tak, zawsze mialem problemy z PDO niby fajne, ale zawsze musialem sie nie zle napocic podenerwowac, zeby ogarnac o co chodzi gdy o cos chodzilo...
Prosilbym o jakakolwiek pomoc jesli to mozliwe...
Poradziłem sobie w ten sposób:
{ $strona = (int) $strona; $strona = $strona - 1; if($strona != 0) $strona = $strona * $limit; $from = 'posty'; $sql = 'SELECT * FROM '.$from.' LIMIT :strona, :limit'; $query = DB::prepare($sql); $query->bindValue(':strona', $strona, PDO::PARAM_INT); $query->bindValue(':limit', $limit, PDO::PARAM_INT); $query->execute(); return $result = $query->fetchAll(); }
ale chcialbym wiedziec dlaczego ten wczesniejszy nie dziala.