User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,220 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,260 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 737 | Replies: 3 | Solved
Reply
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 173
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Help PHP codes to search MySQL

  #1  
Mar 5th, 2008
I'm looking for some PHP scripts that be able to search into the Database and retrieve all the records that matches the user input string in the search form.

Please advise.
"I might not be the BEST but I'm not like the REST!"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: PHP codes to search MySQL

  #2  
Mar 5th, 2008
The script I am posting is a fairly advanced search page using left joins on tables, but I think you can grasp what the script is doing. This search allows for 3 different types of search methods (either used separately or in combination), and allows the user to sort the search results as well. Additionally, this script has restricted access to the page, but you can eliminate that if it is not necessary. I have the results in a repeating row (limited to 50 records per page)This is something I have used, adapted, and built upon several times. If you need the search to be less advanced, you can adapt to your needs as well. Granted, you probably didn't need the whole page of code, but I thought it would be helpful as well.

  1.  
  2. <?php require_once('connections.php'); ?>
  3. <?php
  4. if (!isset($_SESSION)) {
  5. session_start();
  6. }
  7. $MM_authorizedUsers = "";
  8. $MM_donotCheckaccess = "true";
  9.  
  10. // *** Restrict Access To Page: Grant or deny access to this page
  11. function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
  12. // For security, start by assuming the visitor is NOT authorized.
  13. $isValid = False;
  14.  
  15. // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  16. // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  17. if (!empty($UserName)) {
  18. // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
  19. // Parse the strings into arrays.
  20. $arrUsers = Explode(",", $strUsers);
  21. $arrGroups = Explode(",", $strGroups);
  22. if (in_array($UserName, $arrUsers)) {
  23. $isValid = true;
  24. }
  25. // Or, you may restrict access to only certain users based on their username.
  26. if (in_array($UserGroup, $arrGroups)) {
  27. $isValid = true;
  28. }
  29. if (($strUsers == "") && true) {
  30. $isValid = true;
  31. }
  32. }
  33. return $isValid;
  34. }
  35.  
  36. $MM_restrictGoTo = "sendbackto somepagehere.php";
  37. if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
  38. $MM_qsChar = "?";
  39. $MM_referrer = $_SERVER['PHP_SELF'];
  40. if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  41. if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
  42. $MM_referrer .= "?" . $QUERY_STRING;
  43. $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  44. header("Location: ". $MM_restrictGoTo);
  45. exit;
  46. }
  47. ?>
  48. <?php
  49. if (!function_exists("GetSQLValueString")) {
  50. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  51. {
  52. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  53.  
  54. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  55.  
  56. switch ($theType) {
  57. case "text":
  58. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  59. break;
  60. case "long":
  61. case "int":
  62. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  63. break;
  64. case "double":
  65. $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  66. break;
  67. case "date":
  68. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  69. break;
  70. case "defined":
  71. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  72. break;
  73. }
  74. return $theValue;
  75. }
  76. }
  77.  
  78. $maxRows_rsCriteria = 50;
  79. $pageNum_rsCriteria = 0;
  80. if (isset($_GET['pageNum_rsCriteria'])) {
  81. $pageNum_rsCriteria = $_GET['pageNum_rsCriteria'];
  82. }
  83. $startRow_rsCriteria = $pageNum_rsCriteria * $maxRows_rsCriteria;
  84. //$AppName=$row_rsCriteria['app_description'];
  85. mysql_select_db($database_mambo, $mambo);
  86. $query_rsCriteria = "SELECT * FROM users LEFT JOIN access ON users.id = access.user_idLEFT JOIN application_access on access.app_id = application_access.app_id WHERE access.app_id IS NOT NULL AND application_access.app_description IS NOT NULL ";
  87. $query_limit_rsCriteria = sprintf("%s LIMIT %d, %d", $query_rsCriteria, $startRow_rsCriteria, $maxRows_rsCriteria);
  88. $rsCriteria = mysql_query($query_limit_rsCriteria, $mambo) or die(mysql_error());
  89. $row_rsCriteria = mysql_fetch_assoc($rsCriteria);
  90.  
  91. if (isset($_GET['totalRows_rsCriteria'])) {
  92. $totalRows_rsCriteria = $_GET['totalRows_rsCriteria'];
  93. } else {
  94. $all_rsCriteria = mysql_query($query_rsCriteria);
  95. $totalRows_rsCriteria = mysql_num_rows($all_rsCriteria);
  96. }
  97. $totalPages_rsCriteria = ceil($totalRows_rsCriteria/$maxRows_rsCriteria)-1;
  98. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  99. <html xmlns="http://www.w3.org/1999/xhtml">
  100. <head>
  101. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  102. <title>Search User Access</title>
  103. <style type="text/css">
  104. <!--
  105. body,td,th {
  106. font-family: Verdana, Arial, Helvetica, sans-serif;
  107. font-size: 12px;
  108. color: #000066;
  109. }
  110. -->
  111. </style></head>
  112.  
  113. <body>
  114. <form id="search" name="search" method="post" action="">
  115. <?
  116. if($_POST) {
  117.  
  118. $query_rsCriteria = "SELECT * FROM users LEFT JOIN access ON users.id = access.user_idLEFT JOIN application_access on access.app_id = application_access.app_id ";
  119.  
  120. if($_POST['find']!="" && $_POST['findfield']!="") {
  121. $query_rsCriteria .= " WHERE ";
  122. if($_POST['findfield']=="application_access.app_description") {
  123. $query_rsCriteria .= "application_access.app_description LIKE '%" . $_POST['find'] . "%'AND access.app_id IS NOT NULL";
  124. }
  125. if($_POST['findfield']=="users.username") {
  126. $query_rsCriteria.= "users.username LIKE '%" . $_POST['find'] . "%'AND access.user_id IS NOT NULL AND application_access.app_description IS NOT NULL";
  127. }
  128.  
  129.  
  130. /////////////////
  131. if($_POST['find']!="") {
  132. $query_rsCriteria.= "users.username LIKE '%" . $_POST['find'] . "%' AND application_access.app_description IS NOT NULL or application_access.app_description LIKE '%" . $_POST['find'] . "%'";
  133. }
  134. ////////////////
  135. }
  136.  
  137. if($_POST['find']!="" && $_POST['findfield']="") {
  138. $query_rsCriteria .= " WHERE users.username LIKE '%" . $_POST['find'] . "%' or application_access.app_description LIKE '%" . $_POST['find'] . "%'";
  139. }
  140.  
  141.  
  142. if ($_POST['sortby']!=" ") {
  143. $sort=$_POST['sortby'];
  144. $query_rsCriteria .= " ORDER BY $sort";
  145. }
  146. if ($_POST['sortorder']=="Ascending" && $_POST['sortby']!=" ") {
  147. $query_rsCriteria .= " ASC ";
  148. }
  149. if ($_POST['sortorder']=="Descending" && $_POST['sortby']!=" ") {
  150. $query_rsCriteria .= " DESC ";
  151. }
  152.  
  153. if ($_POST['sortby2']!=" ") {
  154. $sort2=$_POST['sortby2'];
  155. $query_rsCriteria .= " , $sort2";
  156. }
  157. if ($_POST['sortorder2']=="Ascending" && $_POST['sortby2']!=" ") {
  158. $query_rsCriteria .= " ASC ";
  159. }
  160. if ($_POST['sortorder2']=="Descending" && $_POST['sortby2']!=" ") {
  161. $query_rsCriteria .= " DESC ";
  162. }
  163.  
  164. if ($_POST['ALL'])
  165. {
  166. $query_rsCriteria = "SELECT * FROM users LEFT JOIN access ON users.id = access.user_idLEFT JOIN application_access on access.app_id = application_access.app_id WHERE access.app_id IS NOT NULL AND application_access.app_description IS NOT NULL AND access.user_idIS NOT NULL ";
  167. }
  168.  
  169. }
  170.  
  171. $rsCriteria = mysql_query($query_rsCriteria, $mambo) or die(mysql_error());
  172. $row_rsCriteria = mysql_fetch_assoc($rsCriteria);
  173. $totalRows_rsCriteria = mysql_num_rows($rsCriteria);
  174. $found = mysql_num_rows($rsCriteria);
  175. ?>
  176. <table width="583">
  177. <tr valign="top">
  178. <th colspan="4" scope="row">User Access to Applications </th>
  179. </tr>
  180. <tr>
  181. <th colspan="2" scope="row"><div align="left">
  182. <input type="button" name="Cancel" value="Return to Menu" onClick="self.location='index.php'" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  183. <input type="button" name="Cancel" value="Add" onClick="self.location='access.php'" /></div></th>
  184. <td width="283" colspan="2"><div align="right"><a href="<?php echo $logoutAction ?>">Log out</a></div></td>
  185. </tr>
  186. </table>
  187. <table width="583" border="1">
  188. <tr valign="top">
  189. <th width="148" scope="row"><div align="right">Primary Sort : </div></th>
  190. <td width="165">
  191. <select name="sortby" id="sortby">
  192. <option value=" "> </option>
  193. <option value="application_access.app_description">Application</option>
  194. <option value="users.username">Username</option>
  195. </select> </td>
  196. <td width="242">
  197. <input name="sortorder" type="radio" value="Ascending" checked="checked" />Ascending
  198. <input name="sortorder" type="radio" value="Descending" />Descending </td>
  199. </tr>
  200. <tr valign="top">
  201. <th scope="row"><div align="right">Secondary Sort: </div></th>
  202. <td>
  203. <select name="sortby2" id="sortby2">
  204. <option value=" "> </option>
  205. <option value="application_access.app_description">Application</option>
  206. <option value="users.username">Username</option>
  207. </select> </td>
  208. <td>
  209. <input name="sortorder2" type="radio" value="Ascending" checked="checked"/>Ascending
  210. <input name="sortorder2" type="radio" value="Descending" />Descending </td>
  211. </tr>
  212. <tr valign="top">
  213. <th scope="row"><div align="right">Find:</div></th>
  214. <td>
  215. <select name="findfield" id="findfield">
  216. <option value=" "> </option>
  217. <option value="application_access.app_description">Application</option>
  218. <option value="users.username">Username</option>
  219. </select> </td>
  220. <td><input name="find" type="text" id="find" /></td>
  221. </tr>
  222. <tr valign="top">
  223. <th colspan="3" scope="row">
  224. <input type="submit" name="Search" value="Search" class="search">
  225. <input type="reset" name="Reset" value="Reset" class="search">
  226. <input type="submit" name="ALL" value="Search All" /></th>
  227. </tr>
  228. </table>
  229. <table width="583" border="1" bordercolor="#000066">
  230.  
  231. <tr>
  232. <td width="85" valign="top" scope="row"><div align="left"><b>Actions</b></div></td>
  233. <td width="132" valign="top" scope="row"><div align="left"><b>Username</b></div></td>
  234. <td width="344" valign="top"><div align="left"><b>Application Access </b>
  235. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  236. &nbsp;&nbsp;</div>
  237. </td>
  238. </tr>
  239. <?php do { ?>
  240. <tr>
  241. <td valign="top" scope="row"><div align="left">&nbsp;
  242. <input name="id" type="hidden" id="id" value="<?php echo $row_rsCriteria['id']; ?>" />
  243.  
  244.  
  245. [<a href="delete.php?id=<?php echo $row_rsCriteria['id'];?>&app_description=<? echo $row_rsCriteria['app_description'];?>&access_id=<? echo $row_rsCriteria['access_id']; ?>">Delete</a>]</div></td>
  246. <td valign="top" scope="row"><div align="left"><?php echo $row_rsCriteria['username']; ?></div></td>
  247. <td valign="top"><div align="left"><?php echo $row_rsCriteria['app_description']; ?>
  248. <input name="user_access_ID" type="hidden" id="user_access_ID" value="<?php echo $row_rsCriteria['access_id']; ?>" />
  249. </div></td>
  250. </tr>
  251. <?php } while ($row_rsCriteria = mysql_fetch_assoc($rsCriteria)); ?>
  252. </table>
  253. </form>
  254.  
  255. </body>
  256. </html>
  257. <?php
  258. mysql_free_result($rsCriteria);
  259. ?>
  260.  
  261.  
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: PHP codes to search MySQL

  #3  
Mar 5th, 2008
try to alter this script for your use:

input page:
  1. <p>Search:</p>
  2. <form name="form1" action="post" action="process.php">
  3. <input type="text" name="text1" value="">
  4. <input type="submit" name="submit" value="Submit">
  5. </form>

process.php
  1. //put your db connection here(for example localhost)....
  2. $host="localhost";
  3. $db_user="user";
  4. $db_password="password";
  5. $database="db";
  6. $dbh=mysql_connect ($host, $db_user, $db_password) or die ('I cannot connect to the database because: ' . mysql_error());
  7. mysql_select_db ("$database");
  8.  
  9. //string to be searched from the input form...
  10. $str=$_POST['text1'];
  11.  
  12. //query string...
  13. $query="SELECT field1,field2 from table1
  14. WHERE field1='$str'";
  15.  
  16. /* field1,field2 are fields you want to display,
  17. table1 is the table where you want to search the data... */
  18.  
  19. $result=mysql_query($query);
  20. $num=mysql_num_rows($result);
  21.  
  22. //display results...
  23. while ($i<$num)
  24. {
  25. $field1=mysql_result($result,$i,"field1");
  26. $field2=mysql_result($result,$i,"field2");
  27. echo $field1;
  28. echo ",";
  29. echo $field2;
  30. echo "</br>";
  31. $i++;
  32. }
  33.  

Basically that's it...
Last edited by ryan_vietnow : Mar 5th, 2008 at 8:52 pm.
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 173
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Re: PHP codes to search MySQL

  #4  
Mar 6th, 2008
Thanks all, solved. used both the codes.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 11:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC