A small change aborts the mysql query

Possible duplicate:
PHP PDO bindValue in LIMIT

This code works for me very well.

if (array_key_exists('cat', $_GET) === TRUE) { $category = $_GET['cat']; } else { $category = '.*'; } $conn = new PDO('mysql:host=localhost;dbname=news', 'root', ''); $stmt = $conn->prepare('SELECT * FROM stories WHERE category RLIKE :cat ORDER BY score DESC LIMIT 0, 25'); $stmt -> execute(array( 'cat' => $category, )); $result = $stmt->fetchAll(); 

As you can see, he gets the category from the request for receipt and looks for a database for everything in this category.

I am also trying to add a bit so that the page can be specified in the get request and the request will start 25 lines later for each increment by one on the page.

Here is what I wrote:

 if (array_key_exists('cat', $_GET) === TRUE) { $category = $_GET['cat']; } else { $category = '.*'; } if (array_key_exists('page', $_GET) === TRUE) { $page = intval($_GET['page'])*25; } else { $page = 0; } $conn = new PDO('mysql:host=localhost;dbname=news', 'root', ''); $stmt = $conn->prepare('SELECT * FROM stories WHERE category RLIKE :cat ORDER BY score DESC LIMIT :page, 25'); $stmt -> execute(array( 'cat' => $category, 'page' => $page )); $result = $stmt->fetchAll(); 

But now the query returns nothing, regardless of which page or if there is a category.

Perhaps I am not entitled to integers. Any idea why I get this result?

+4
source share
2 answers

As in the example on how to apply values ​​to LIMIT conditions :

 $stmt -> execute(array( 'cat' => $category, 'page' => (int) $page )); 
0
source

Ray has already given you the answer: pass the page variable provided by the user into an integer (be sure to do this, or you will be vulnerable to SQL injection) and insert it directly into the query string, without using a placeholder.

-2
source

Source: https://habr.com/ru/post/1442828/


All Articles