Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (28)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (27)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
How To Build WHERE Criteria with Web Form Search Fields
How To Build WHERE Criteria with Web Form Search Fields? - MySQL FAQs - Managing Tables and Running Queries with PHP Scripts
✍: FYIcenter.com
If your PHP script is linked to a Web form which takes search key words for multiple data fields. For example, your Web form asks your visitor to search for Website links with a URL search field, a Website title search field, a description search field, and a comment search field.
Now you have to build a nice WHERE criteria string that meets the following requirements:
The tutorial script below shows you a good sample that meets the above requirements:
<?php $_REQUEST = array("title"=>" Joe's brother\'s ", "description"=>"c:\windows\system ", "comment"=>" best "); $sql = "SELECT * FROM siteLinks WHERE 1=1"; $url = getFormParam("url"); $title = getFormParam("title"); $description = getFormParam("description"); $comment = getFormParam("comment"); if (strlen($url) > 0) $sql .= " AND url LIKE '%".$url."%'"; if (strlen($title) > 0) $sql .= " AND title LIKE '%".$title."%'"; if (strlen($description) > 0) $sql .= " AND description LIKE '%".$description."%'"; if (strlen($comment) > 0) $sql .= " AND comment LIKE '%".$comment."%'"; print("SQL statement:\n"); print($sql."\n"); function getFormParam($p) { if (isset($_REQUEST[$p])) { return str_replace("\\", "\\\\", str_replace("'", "''", trim($_REQUEST[$p]))); } else { return ""; } } ?>If you run this script, you will get something like this:
SQL statement: SELECT * FROM siteLinks WHERE 1=1 AND title LIKE '%Joe''s brother\\''s%' AND description LIKE '%c:\\windows\\system%' AND comment LIKE '%best%'
You should learn a couple of things in this script:
2007-05-11, 6101👍, 0💬
Popular Posts:
What is the difference between mysql_fetch_object() and mysql_fetch_array() functions in PHP? mysql_...
What is the method to customize columns in DataGrid? Use the template column.
Which are the various programming approaches for WCF?
How Do I Run JUnit Tests from Command Window? To run JUnit tests from a command window, you need to ...
.NET INTERVIEW QUESTIONS - Did VB6 support multi-threading ? While VB6 supports multiple single-thre...