PHP-MySQL query -- Please explain
Ran across this, but I cannot quite grasp it, especially beginning with the LIMIT phrase. Thanks for any help.
$sql = sprintf ("SELECT *
开发者_如何学JAVA 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.'%') );
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?
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.
精彩评论