473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to search the mysql database using php

127 100+
thjis is my search form...

Expand|Select|Wrap|Line Numbers
  1. <form name="form" action="search.php" method="get">
  2.   <input type="text" name="q" />
  3.   <input type="submit" name="Submit" value="Search" />
  4. </form>
  5.  
this is my search.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. include('lock.php');
  4. include("config.php");
  5.  
  6.  
  7.  
  8.   $var = @$_GET['q'] ;
  9.   $trimmed = trim($var); 
  10.  
  11.  
  12.  
  13.  
  14.  
  15. // rows to return
  16.  
  17.  
  18.  
  19.  
  20. if ($trimmed == "")
  21.   {
  22.   echo "<p>Please enter a search...</p>";
  23.   exit;
  24.   }
  25.  
  26.  
  27. /*if (!isset($var))
  28.   {
  29.   echo "<p>We dont seem to have a search parameter!</p>";
  30.   exit;
  31.   }*/
  32.  
  33. $query = "select * from candidate where cname like \"%$trimmed%\" ||  skills like \"%$trimmed%\" ||   exp like \"%$trimmed%\" || indus like \"%$trimmed%\" || qual like \"%$trimmed%\" ORDER BY cid ";
  34.  
  35. $result=mysql_query($query);
  36. $num=mysql_numrows($result);
  37.  
  38.  
  39.  
  40. if ($num == 0)
  41.   {
  42.   echo "<h4>Results</h4>";
  43.   echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
  44.  
  45.  }
  46.  
  47.  
  48. echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
  49. echo "Results";
  50. print "<table width='100%' border='1'>";
  51. print "<tr><th align='center'>Name</th><th align='center'>Email</th><th align='center'>Qualification</th><th align='center'>Skills</th><th align='center'>Resume</th></tr>";
  52. $i=0;
  53. while ($i < $num) {
  54. $cname=mysql_result($result,$i,"cname");
  55. $email=mysql_result($result,$i,"email");
  56. $skills=stripslashes(mysql_result($result,$i,"skills"));
  57. $qual=mysql_result($result,$i,"qual");
  58. $id=mysql_result($result,$i,"cid");
  59. $filename=mysql_result($result,$i,"res_title");
  60. print "<tr>
  61. <td>$cname</td>
  62. <td>$email</td>
  63. <td>$qual</td>
  64. <td>$skills</td>
  65. <td><img src='download.gif'>&nbsp;<a href='download.php?id=$filename'>$filename</a></td>
  66. <td><a href='mailto:'>Forward</a></td>
  67. <td><a href='updatestatus.php'>Update</a></td>
  68. </tr>";
  69. $i++;
  70. }
  71. print "</table>";
  72. ?>
  73.  
i want search multiple values from the table. for example,

if i enter a search like " be java", then,

i have to search the db for 'be' key word and 'java' keyword seperately. then the result should be the candidates who having qualification 'be' and skills 'java' only to be displayed... how? help...
Jul 30 '10 #1
6 2852
code green
1,726 Expert 1GB
You can use implode() or strtok() to split the words into array elements.
Then loop through the array creating a query with that part as the search string.
Jul 30 '10 #2
Atli
5,058 Expert 4TB
If you want to search for multiple values in a single field, a simple method is to use regular expressions. Note, this is not the most efficient method, but it does work.

For example, if I wanted to search for both "test" and "example" in a field I could do:
Expand|Select|Wrap|Line Numbers
  1. SELECT `stuff` FROM `myTable`
  2. WHERE `myCol` REGEXP '[[:<:]](test|example)[[:>:]]';
  3.  
This would return any field that had those two words anywhere within. The "[[:<:]]" and "[[:>:]]" parts will prevent it from matching the words within other words. If you don't want that, simply remove them.

(See 11.5.2. Regular Expressions for more info on how to construct more complex regular expressions)

If your search queries are coming in as space separated keywords, then all you would have to do is replace the space with a (|) and you put it into the query. The str_replace function could help you with that.

P.S.
Be sure to secure the input before using it though!
Aug 1 '10 #3
impin
127 100+
i want to search multiple words in multiple fields in the table....
Aug 2 '10 #4
Atli
5,058 Expert 4TB
What I posted was an example. I showed you how you can use a regular expression to search a column. That should make it easy for you to build the query you need, based on what you already have.

I'm not going to write the whole thing for you.
Aug 2 '10 #5
impin
127 100+
i dont want your code............. go
Aug 3 '10 #6
try "exploring" on php.net the php ... explode = " " function. This will seperate words that are placed together. This is what my PHP for Dummies says in Ch. 13explode ('sep", "string"): Creates an array of strings in wich each item i a substring of strings, separated by sep. For example, explode(" ",$string) creates an array in wich each wrd in $string is a separate value. This is similar to split in Perl.
Nov 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: linus | last post by:
Anyone try insert image or binary files to MySQL using ASP.NET? Thanks a lot
1
by: Devendra | last post by:
How could i insert a pdf file in mysql using PHP and when i access from database it must be open in .html format plz give me reply
0
by: Ginoboy | last post by:
Hello there I have a problem in executing mysql using java netbeans. all i want is to execute a batch file. this is my code: "try { ...
1
by: mohammedsk | last post by:
Hi, I am trying to save information into MySQL using PHP. The data I am saving is in Arabic language. The database table will show some "???" or other characters. If I insert the text directly...
1
by: ariel gons | last post by:
This is one of my project and also my thesis in school. How can I get data on database MYSQL using javascript function? Is there anybody here can help me with this problem.. Can anyone give...
1
by: chirag thakor | last post by:
how i can import data from Excel to MySql using php.
3
by: blackevanuz | last post by:
Hi I want to storage a txt file into mysql using a blob and after that extract it, the code i use to save it is: fs = new FileStream("C:\\Documents and Settings\\Pru.txt",...
2
by: shasia | last post by:
i insert an image into mysql using asp.net.but only null values are stored in the mqsql .i dont know why?
9
paulrajj
by: paulrajj | last post by:
hi everybody, i am newbie to php and mysql. i have a little bit knowledge about php with xml. how to insert and select the records from xml to mysql using php?
3
by: emsik1001 | last post by:
Hi http://dev.mysql.com/doc/query-browser/en/mysql-query-browser-connection.html I'm trying to connect from my Windows based PC to a dedicated server which is running on Linux system. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.