473,387 Members | 3,820 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,387 software developers and data experts.

search database

Hi
I am fairley new at this, and trying to search my database with mutiple questions and to return the exact answer instead of listing everything in the query, ie if I search for blue vw golf I want it to return all the blue vw golf not all the vw', all the blue's and all the golf's. Can anyone help ?

Steve
Dec 22 '10 #1

✓ answered by AutumnsDecay

Are you searching from a user accessible form?

If it's on a back-end of a website, you could just make yourself a form that has two 'select' boxes. The options of the select box could be the available options of colors and makes you want to offer. Example:

-------FORM-------

Choose your Make: _______________
|_Option 1: VW
|_Option 2: Chrysler
|_Option 3: Etc...

Choose your Color: _______________
|_Option 1: Blue
|_Option 2: Black
|_Option 3: Etc...

-----END FORM-----

Then compile your MySQL statement based on the selections the user has put in.

Expand|Select|Wrap|Line Numbers
  1. $color = $_POST['color'];
  2. $make = $_POST['make'];
  3.  
  4. $query = "SELECT * FROM cars WHERE color = '$color' AND make = '$make'";
  5.  

13 1808
AutumnsDecay
170 100+
MySQL allows you to use 'AND' or 'OR within you 'WHERE' argument.

Your statement would look something like:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM cars WHERE color = 'Blue' AND make = 'VW';
  2.  
This would select all blue VWs in your 'cars' table.

Replace the information I've populated with your own, ie. if your table isn't named 'cars', switch that to what it is called. Same with 'color' and 'make'.
Dec 22 '10 #2
thank you, but what would i have to put if i wanted a red one, would i have to keep duplicating same string ?
Dec 22 '10 #3
AutumnsDecay
170 100+
Are you searching from a user accessible form?

If it's on a back-end of a website, you could just make yourself a form that has two 'select' boxes. The options of the select box could be the available options of colors and makes you want to offer. Example:

-------FORM-------

Choose your Make: _______________
|_Option 1: VW
|_Option 2: Chrysler
|_Option 3: Etc...

Choose your Color: _______________
|_Option 1: Blue
|_Option 2: Black
|_Option 3: Etc...

-----END FORM-----

Then compile your MySQL statement based on the selections the user has put in.

Expand|Select|Wrap|Line Numbers
  1. $color = $_POST['color'];
  2. $make = $_POST['make'];
  3.  
  4. $query = "SELECT * FROM cars WHERE color = '$color' AND make = '$make'";
  5.  
Dec 22 '10 #4
Hi
no I do not want to go down the route of having different check boxes, it is just a search box, not sure if I have to put a weighted search in but have no idea of how to acomplish that, or if it needs to search different tables, and again not sure how to set that up
Dec 23 '10 #5
AutumnsDecay
170 100+
Essentially this would just be two boxes side by side.

A standard search box IS a form element with (usually) one text input and a submit button.

All we're doing here is creating a textbox that has set values. Not only is this an excellent choice for the type of MySQL you want to run, most users prefer this as they're having to guess what models you have available. The list will be right in front of them in the form of a multiple choice select box.
Dec 23 '10 #6
Hi
thankyou for your reply but this is not the route I want to go down, I do not want a form

Thank You
Merry Christmas
Dec 24 '10 #7
Rabbit
12,516 Expert Mod 8TB
You don't want a form? A "search box" as you call it is a form.

We have no idea how your table is structured. That affects how you will be able to search for records.
Dec 24 '10 #8
ok ... I do not want radio buttons, I do not want drop down lists, all I want is the one "form" box with a submit on it the will search whatever query that is entered into it and return the correct result... not a list of variables
Dec 26 '10 #9
Rabbit
12,516 Expert Mod 8TB
Be that as it may, you still have not told us how your database is structured. As I said, how it is structured will limit how you can query it.
Dec 27 '10 #10
I have 6 coulombs, id type make model colour and image ...but this is one of the questions do I need to change the table in order to achieve the result ? does it need to be weighted ?
Dec 28 '10 #11
Rabbit
12,516 Expert Mod 8TB
I'm not sure what you mean by weighted. The layout is fine. The question now is what is the structure of the input? Will it always be color make model in that order?

I don't know MySQL but I believe it has a match against keyword that you can use.
Dec 28 '10 #12
AutumnsDecay
170 100+
Here's the issue.

MySQL queries are structured, hence the name 'Structured Query Language'.

As mentioned earlier, a typical MySQL statement would be

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM cars WHERE color = 'red' AND make = 'VW'
The PHP to make this be able to be dynamic is:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM cars WHERE color = '$color' AND make = '$make'
Now you have to ask, how is $make and $color obtaining it's values? Via user input through a single text box? Okay, well what potential issues might we run into? Well what if the user puts in "vw red" as a search? Even if we made a PHP code to select the first word in the box as the color, and the second word as the make (ie, red vw), we're still trusting that the user will be smart enough to read instructions.

So let's say the user enters 'vw red' as a search, our PHP take the first word and make it '$color' and then it takes the second word and makes it '$make', Well in this case that's wrong, as our SQL statement will then end up quering the database like so:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM cars WHERE color = 'vw' AND make = 'red'
Which will not work. What you could TRY doing (if you really are set on ONE textfield) is adding a 'description' to your SQL table for each item, and have the query search for all cars with 'red' and 'vw' in the description field. But again, that's a pretty bad way of doing it.

If you like at 95% of the autodealer sites, you'll see that they offer drop down options for this very reason. There's too many variables for a standard user to get right, so you should define them for the user wherever possible. This would be one of those times.
Dec 28 '10 #13
hi
got it sorted thanks .... using explode implode and using the function of using + as to add the words together in the scripting of the search

thank you for your help

kind regards and happy new year

Steve
Dec 29 '10 #14

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

Similar topics

2
by: mp | last post by:
Hi, Is posible to make incremental search of some databse (table) with c#. I am looking for some example. I know how to do that with Delphi, but I am new in C# .NET and I need your help. ...
2
by: Guoqi Zheng | last post by:
Dear sir, I have over 2000 products with detail information on database. I would like to offer our visitors an advanced search function to search our database. Currently, I am using the like %...
4
by: visionstate | last post by:
Hi there, I'm fairly new to access and the way it works and I have been building a relatively simple database. It is basically a database where staff members can search for what training they (or...
3
by: vonclausowitz | last post by:
Hi All, I was thinking of creating a table in my database to index all words in the database. That way I can quickly search for one or more words and the index table will return the words and...
2
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hello, I know that this question might need to also be in the SQL forum, but I'm not sure where I need to code this. So here it is. I need to create a search textbox that allows users to type...
0
by: ieyra204 | last post by:
hi, i wanna know how to search the data in PC by using GSM modem and rabbit microprocessor. i use to make a system for student to check their result using sms. i already make a database in...
1
by: danner | last post by:
I am using Visual Basic 2005 Express Edition. I have used it to create the database.. SQL Server Database file CodeDatabase.mdf My experience is outdated.. from QBasic over a decade ago. I am...
9
greensleeves
by: greensleeves | last post by:
Hi, Im a newbie to PHP and I'm wondering if someone could help me out with this Tryng to search out products from a database by clicking a checkbox option for instance if description contains...
1
by: romtang | last post by:
Tring to get help with a couple things. First thing is a good Search Engine (not like yahoo, google etc.) that searches my database somehow. A search engine that could search by 1 or more...
4
by: cewynn | last post by:
I am a beginner working in visual studio 2005 and I created an application to keep track of my movie database. I created 2 forms, 1st form has my movies listed and 2 form is a search form. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...

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.