472,325 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 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 2768
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...
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...
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...
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...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.