PHP Note Learning Excerpt (Genesis new article)

Source: Internet
Author: User

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
  1. <?php
  2.      $conn  =  @mysql_connect ( "localhost" ,&NBSP; "root" ,&NBSP; or die " Database link Error " " &NBSP;&NBSP;
  3. mysql_select_db ("BBS", $conn);
  4. mysql_query ("Set names ' GBK '"); //Use GBK Chinese code;   
  5.       
  6. if($_get[' key ']) {
  7.           
  8. $sql = "SELECT * from ' the text ' WHERE content like '%$_get[key]% '";
  9. $query = mysql_query ($sql);
  10.           
  11. While ($r=mysql_fetch_array ($query)) {
  1. echo "$r [content]". "<br>"  ;
  2.         }  
  3.     }  
  4.       
  5. ?>
  6.   
  7. <body>
  8. <form action="" method="get">
  9. Key Words:
  10. <input type="text" name="key" />
  11. <input type="Submit" name="Sub" value="search" />
  12. </form>
  13. </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
  1. <?php
  2.      $conn  =  @mysql_connect ( "localhost" ,&NBSP; "root" ,&NBSP; or die " Database link Error " " &NBSP;&NBSP;
  3. mysql_select_db ("BBS", $conn);
  4. mysql_query ("Set names ' GBK '"); //Use GBK Chinese code;   
  5.       
  6.     if ( $_get
  7. $k = explode("", $_get[key]);
  8.           
  9. $sql = "SELECT * from ' text ' WHERE content like ' percent $k [0]% ' or content like '% $k [1]% '] ;
  10. $query = mysql_query ($sql);
  11.           
  12. While ($r=mysql_fetch_array ($query)) {
  13.               $r [content] = preg_replace ( "/($k [0])/i" ,&NBSP; "<font color=red><b>\\1</b></font>" ,&NBSP; $r [content]);   
  14.               $r [content] = preg_replace ( "/($k [1])/i" ,&NBSP; "<font color=red><b>\\1</b></font>" ,&NBSP; $r [content]);   
  15. echo "$r [content]". "<br>"  ;
  16.         }  
  17.     }  
  18.       
  19. ?>
  20.   
  21. <body>
  22. <form action="" method="get">
  23. Key Words:
  24. <input type="text" name="key" />
  25. <input type="Submit" name="Sub" value="search" /> /c5>
  26. </form>
  27. </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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.