If, True,false,return value.
fuzzy lookups in PHP's SQL statements
Fuzzy lookups are implemented primarily through the like (case-insensitive) keyword. Like conditions are generally used when specifying a field to search for, by "%" or "_" wildcard function to implement Fuzzy Lookup function, the wildcard can be in front of the field can also be in the back or before and after. It is not possible to find a template through like, so the function of wildcards cannot be ignored.
Here are three examples: Search begins with PHP:
SELECT * FROM table WHERE title like ' php% '
Search ends with PHP:
SELECT * FROM table WHERE title like '%php '
Search contains PHP100:
SELECT * FROM table WHERE title like '%php% '
Note:% represents a string of 0 or more characters, _ represents a single character, similar to the action of a meta-character in a regular expression, you can add additional conditions after like, and the contents of a database similar to the IF statement example
[PHP]View Plain Copy
- <?php
- $conn = @mysql_connect ( "localhost" ,&NBSP; "root" ,&NBSP; or die " Database link Error " " &NBSP;&NBSP;
- mysql_select_db ("BBS", $conn);
- mysql_query ("Set names ' GBK '"); //Use GBK Chinese code;
-
- if($_get[' key ']) {
-
- $sql = "SELECT * from ' the text ' WHERE content like '%$_get[key]% '";
- $query = mysql_query ($sql);
-
- While ($r=mysql_fetch_array ($query)) {
- echo "$r [content]". "<br>" ;
- }
- }
-
- ?>
-
- <body>
- <form action="" method="get">
- Key Words:
- <input type="text" name="key" />
- <input type="Submit" name="Sub" value="search" />
- </form>
- </body>
Search for PHP
Two, multiple keyword search principles and techniquesSteps for a single keyword search: Submit form->php File receive keyword, execute sql-> output If it is a multi-keyword: When you submit a form, multiple keywords are separated by a space or a + sign, and then the PHP file receives the submitted string. Some functions, such as the Explore function, split a string into multiple keywords and store it in an array, and then you can find what you're looking for in the database through multiple like blur lookups.
third, replace the keyword highlightingAfter you get what you want with a fuzzy lookup, use regular expressions to replace what you're looking for, and you can change the effect of the display by changing the color, bold, and so on.
[PHP]View Plain Copy
- <?php
- $conn = @mysql_connect ( "localhost" ,&NBSP; "root" ,&NBSP; or die " Database link Error " " &NBSP;&NBSP;
- mysql_select_db ("BBS", $conn);
- mysql_query ("Set names ' GBK '"); //Use GBK Chinese code;
-
- if ( $_get
- $k = explode("", $_get[key]);
-
- $sql = "SELECT * from ' text ' WHERE content like ' percent $k [0]% ' or content like '% $k [1]% '] ;
- $query = mysql_query ($sql);
-
- While ($r=mysql_fetch_array ($query)) {
- $r [content] = preg_replace ( "/($k [0])/i" ,&NBSP; "<font color=red><b>\\1</b></font>" ,&NBSP; $r [content]);
- $r [content] = preg_replace ( "/($k [1])/i" ,&NBSP; "<font color=red><b>\\1</b></font>" ,&NBSP; $r [content]);
- echo "$r [content]". "<br>" ;
- }
- }
-
- ?>
-
- <body>
- <form action="" method="get">
- Key Words:
- <input type="text" name="key" />
- <input type="Submit" name="Sub" value="search" /> /c5>
- </form>
- </body>
Of course, proficiency in PHP, to practice code, PHP also has many integrated development environment, can also implement many software projects
PHP Note Learning Excerpt (Genesis new article)