tags:

views:

57

answers:

2

Ran across this, but I cannot quite grasp it, especially beginning with the LIMIT phrase. Thanks for any help.

$sql = sprintf ("SELECT * 
                   FROM $tbl_name  
                   WHERE title LIKE '%s' OR description LIKE '%s' 
                   LIMIT $start, $limit",
                         mysql_real_escape_string('%'.$search_term.'%'),
                         mysql_real_escape_string('%'.$search_term.'%') );
+1  A: 

The Limit keyword allows you to select 'pages' of records, for example when you want records 26 through 50. The sprintf function takes a string with placeholders (%s,etc) and the variables to insert in those placeholders. Does that clear things up?

Fosco
Thank you for answering this question. Yes, it is much more clear now.
dave
+1  A: 

The sprintf function grabs the first argument as a string and as Fosco already explained - the %s's are basically placeholders (%s are for strings, there are other abbrevations as well, but lets not get into that right now). All but the first argument of the sprintf function acts like the replacement content for the placeholders. The first argument (skipping the actual first, the string, making it the second argument if you will) replaces the first placeholder with the proper content, the second (actual 3rd) argument would replace the second placeholder and so on.

The % characters within the second and third arguments acts like a any character character in a SQL query. Basically what it does is that it allows the search term to be between any characters. If the second argument would of have looked like '%'.$search_term it would only allow characters in front of the search term, but not behind.

Fosco also explained the usage of LIMIT pretty well I suppose, thus me not getting deeper into that specific area.

Chris
Thank you, Chris, for this extend response. This is very clear, indeed.
dave
You're welcome. I'm just glad I could help! :)
Chris