473,591 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to search the mysql database using php

127 New Member
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>
this is my search.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  3. include('lock.php');
  4. include("config.php");
  8.   $var = @$_GET['q'] ;
  9.   $trimmed = trim($var); 
  15. // rows to return
  20. if ($trimmed == "")
  21.   {
  22.   echo "<p>Please enter a search...</p>";
  23.   exit;
  24.   }
  27. /*if (!isset($var))
  28.   {
  29.   echo "<p>We dont seem to have a search parameter!</p>";
  30.   exit;
  31.   }*/
  33. $query = "select * from candidate where cname like \"%$trimmed%\" ||  skills like \"%$trimmed%\" ||   exp like \"%$trimmed%\" || indus like \"%$trimmed%\" || qual like \"%$trimmed%\" ORDER BY cid ";
  35. $result=mysql_query($query);
  36. $num=mysql_numrows($result);
  40. if ($num == 0)
  41.   {
  42.   echo "<h4>Results</h4>";
  43.   echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
  45.  }
  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. ?>
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 2865
code green
1,726 Recognized Expert Top Contributor
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
5,058 Recognized Expert Expert
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)[[:>:]]';
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.

Be sure to secure the input before using it though!
Aug 1 '10 #3
127 New Member
i want to search multiple words in multiple fields in the table....
Aug 2 '10 #4
5,058 Recognized Expert Expert
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
127 New Member
i dont want your code........... .. go
Aug 3 '10 #6
4 New Member
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

by: linus | last post by:
Anyone try insert image or binary files to MySQL using ASP.NET? Thanks a lot
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
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 { Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysql.exe mysql -u root -p -D gino < C:\\projects\\sample\\gino.txt"); } catch(Exception e)
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 through PHPMyAdmin it works, but not through PHP. I also, modified the PHP page to accept UTF-8 encoding. What other changes do I have to do? Thanks in advance,
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 me a sample code for this.. Please help me with this.
by: chirag thakor | last post by:
how i can import data from Excel to MySql using php.
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", FileMode.Open, FileAccess.Read); FileSize = fs.Length; rawData = new byte; fs.Read(rawData, 0, Convert.ToInt32(FileSize)); fs.Close(); SQL = "INSERT INTO EEG (eeg)values ('" + rawData...
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?
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?
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 type server host and port 3306 user name admin + password and default schema and I get access denied message. I use putty to connect to the server using IP and port 22 and it connects to the server.
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.