Sidetik
Sidetik

Reputation: 620

MySQL LIKE and LIMIT

I am trying to select stuff from a database with the LIKE statement, but I would also like to LIMIT the amount of records I get out of it.

SELECT * FROM proizvodi WHERE `naziv` LIKE %127%
AND LIMIT 0, 10

The code I have is a boolean, and is not working. How to fix this?

Upvotes: 1

Views: 9027

Answers (7)

19ucs028 cnkarthick
19ucs028 cnkarthick

Reputation: 1

change the query to this below SELECT * FROM proizvodi WHERE naziv LIKE %127% LIMIT 0, 10

Upvotes: -1

Eldarni
Eldarni

Reputation: 425

Look at the documentation for the SELECT statement, you don't need to use AND to join your WHERE and LIMIT sections.

See: http://dev.mysql.com/doc/refman/5.7/en/select.html

SELECT * FROM proizvodi WHERE `naziv` LIKE %127% LIMIT 0, 10;

Upvotes: 2

Sidetik
Sidetik

Reputation: 620

Strings in SQL need to be quoted and the LIMIT clause cannot be joined with AND.

SELECT * FROM proizvodi WHERE `naziv` LIKE '%127%' LIMIT 0, 10

Upvotes: 3

You can try something like:

$sql='SELECT * FROM product WHERE `ProName` LIKE "%'.$searchWord.'%" LIMIT '.$this_page_first_result.', '.$results_per_page.';'

Upvotes: 0

Xfile
Xfile

Reputation: 684

$query = mysqli_query($connect,
         "SELECT * FROM proizvodi
         WHERE `naziv` LIKE %127%
         ORDER BY naziv ASC
         LIMIT 10");

This will limit your queries up to 10.

$start, $per_page is something to get hands later if this is a problem now.. :)

Upvotes: -1

Gordon Linoff
Gordon Linoff

Reputation: 1271003

SQL is not guaranteed to return the same results in the same order each time, unless you use an order by. You code should look like:

SELECT *
FROM proizvodi
WHERE `naziv` LIKE %127%
ORDER BY <something>
LIMIT $start, $per_page

Upvotes: 1

Jimmy T.
Jimmy T.

Reputation: 4190

Limit is not part of the where clause and therefore no AND is allowed.

Upvotes: 0

Related Questions