FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Searching MySQL Database





Shike
Ok, Here's my question. I'm working on a database that I want to have a search form for. I was wondering if MySQL supported the "and, or, not" type searches (through a php script). I'm knew at both PHP and MySQL and am taking steps to learn.

as a side note.

Library Catalogs allow for wild card searching (using either a * or ?) as well as phrase searching using quote marks. Is it possible to program these abilities into my script?

Right now I'm not looking for a "How do I?" but a "Can I?"

Thanks.
hexkid
Shike wrote:
[...] I was wondering if MySQL supported the "and, or, not" type searches (through a php script).

[...]

Library Catalogs allow for wild card searching (using either a * or ?) as well as phrase searching using quote marks. Is it possible to program these abilities into my script?


Yes to both questions.

You may have to tweak the ANDs, ORs, and NOTs to get exactly what you want from the database (which may prove difficult if they're provided by the end-user).

As an extension, MySQL provides regular expressions which may prove more suitable to your needs.

Examples that select all districts in Portugal without the letter 'o' in their name
Code:
mysql> select * from distrito where descricao not like '%o%';
+------------+--------------+
| IDdistrito | descricao    |
+------------+--------------+
|          3 | Viseu        |
|          7 | Beja         |
|          8 | Braga        |
|          9 | Braganca     |
|         12 | Guarda       |
|         13 | Leiria       |
|         15 | Santarem     |
|         16 | Setubal      |
|         18 | Vila Real    |
|         20 | R.A. Madeira |
+------------+--------------+
10 rows in set (0.00 sec)

mysql> select * from distrito where descricao regexp '^[^o]+$';
+------------+--------------+
| IDdistrito | descricao    |
+------------+--------------+
|          3 | Viseu        |
|          7 | Beja         |
|          8 | Braga        |
|          9 | Braganca     |
|         12 | Guarda       |
|         13 | Leiria       |
|         15 | Santarem     |
|         16 | Setubal      |
|         18 | Vila Real    |
|         20 | R.A. Madeira |
+------------+--------------+
10 rows in set (0.00 sec)

Traveller
Shike wrote:
Ok, Here's my question. I'm working on a database that I want to have a search form for. I was wondering if MySQL supported the "and, or, not" type searches (through a php script). I'm knew at both PHP and MySQL and am taking steps to learn.

Absolutely. MySQL is SQL, and SQL is a complete and powerful language for database work, including queries (i.e. searches). It's a little different from just using a search engine, though, so it would be a good idea to check out a site such ashttp://www.w3schools.com/sql/sql_intro.asp. You could try the actual MySQL site (http://www.mysql.com), but the w3schools site is probably a little easier for starting out.

Shike wrote:
Library Catalogs allow for wild card searching (using either a * or ?) as well as phrase searching using quote marks. Is it possible to program these abilities into my script?

As hexkid mentioned, MySQL even has "regular expression" capabilities but, yes, it does include regular wild cards, as well. As you go through the above tutorial, I'm sure you'll find enough to get you started with wild cards, as well.
Shike
Thanks guys. This stuff is great. Again, I'm not to the stage of actually working with SQL or PHP yet (as i'm still learning the PHP). I actually created the search forms (which I hope work Smile ).

I will definately be posting here with more questions in the future though.
Related topics
Cron Job to Backup MySQL Database
Mysql database problem
PHPBB MYSQL Database Question.
MySQL database for a forum
Mysql database size?
Connect to MySql database
[MySQL database] Password and username
insert data to mySQL database problem
MySQL Database!?
Delete data from MySQL database problem!!
MySQL database
Problem connecting to MySQL database with PHP [RESOLVED]
How to search in a MySQL - database
mysql database table structure
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.