Alex Maina
Alex Maina

Reputation: 306

search for multiple keywords with php and mysql (where X like) set Limit

I would like to know how one can set limits to the queries suggested here and here

For example where can i set a Limit to the solution suggested by @Ibu?

$queried = mysql_real_escape_string($_POST['query']); // always escape

$keys = explode(" ",$queried);

$sql = "SELECT * FROM links WHERE name LIKE '%$queried%' ";

foreach($keys as $k){
    $sql .= " OR name LIKE '%$k%' ";
}

$result = mysql_query($sql);

I have tried setting it on the second $sql variable like this:

$sql .= " OR name LIKE '%$k%' LIMIT 0, 20 ";

However, this returns no results. Thank you in advance for your input.

Upvotes: 0

Views: 98

Answers (1)

Moe
Moe

Reputation: 139

You shouldn't be using mysql functions but instead use mysqli however this is what you are looking for

$sql = "SELECT * FROM links WHERE name LIKE '%$queried%' ";
foreach($keys as $k){
    $sql .= " OR name LIKE '%$k%' ";
}
$sql .= "LIMIT 0, 20";
$result = mysql_query($sql);

Upvotes: 1

Related Questions