Searching entire database...

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Which MySQL query does one run to search the entire database and not just one column... is it even possible? Or maybe not which, but what query. It's probably SELECT something, but what comes after that?

Busy's picture

He has: 6,151 posts

Joined: May 2001

Select * from db name

* = everything, but really you need a condition; where something equals or like something

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Oh yeah, I got that Busy, but, what I'm talking about is a string in the entire database.

I realise it's SELECT * FROM WHERE = ;

But, that only searches one column, how do you search the same string in all the columns of the database?

Understand what I'm talking about?

Busy's picture

He has: 6,151 posts

Joined: May 2001

gotcha, sorry was late ...

I've only ever searched through two or three at one time so just included all three conditions in the code

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

That's sounds like a very unusual thing to do. Normally fields contain very distinct types of data (hence they are distinct fields). Why are you trying to do this?

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I'm trying to do this because I'm wanting to search for a string in a table, the site I'm working on at the moment relies on the ability of the user to search for the information that they need...

So, what I'm trying to do is not possible?

chrishirst's picture

He has: 379 posts

Joined: Apr 2005

Yes it's possible, but only with MySQL 5.0 and then only if the tables are ISAM and have been set up with FULLTEXT indexes.

Other than that you will have to set up your SQL command to search specific columns, otherwise you will have errors when a text search is done against a non-text field

Chris

Indifference will be the downfall of mankind, but who cares?
Venue Capacity Monitoring
Code Samples

They have: 10 posts

Joined: Nov 2005

chrishirst wrote: Yes it's possible, but only with MySQL 5.0 and then only if the tables are ISAM and have been set up with FULLTEXT indexes.

Other than that you will have to set up your SQL command to search specific columns, otherwise you will have errors when a text search is done against a non-text field

chrishirst solution is the best I guess... the Full text is working in mysql 4.0 just set the fields you want to be include in the query as FULL TEXT.

Then here is a query sample:

Quote:
$sql = "SELECT * FROM WHERE MATCH (,,,) AGAINST ('%$query%')";

$query is variable

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Well, I'm not too sure what version of MySQL my webhost is running, I don't think it's 5.0 though.

The tables are ISAM and are all fields except two are VARCHAR Plain

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

How many fields are there that you are trying to search in?

WHERE
   field1 LIKE "%criteria%" OR
   field2 LIKE "%criteria%" OR
   field3 LIKE "%criteria%" OR
   field4 LIKE "%criteria%" OR
   field5 LIKE "%criteria%"
'
You could try something like this, gets tideous if you have a lot of fields.

Can you give a structure of the tabe here, may be able to suggest something else that is not only more simple, but that puts less load on the database server as well.

-Greg

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Thanks Greg, here is what PHPMyAdmin spat out when I created the table:

CREATE TABLE `club` (
`userID` INT NOT NULL AUTO_INCREMENT ,
`sport` VARCHAR( 255 ) NOT NULL ,
`location` VARCHAR( 255 ) NOT NULL ,
`name` VARCHAR( 255 ) NOT NULL ,
`number` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) ,
`website` VARCHAR( 255 ) ,
`details` TEXT,
PRIMARY KEY ( `userID` )
) TYPE = MYISAM ;
'

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Probably the code I gave you (or similar) would work the best.

-Greg

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Great, thanks Greg Smiling

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

What happens if the query is empty... how do I do a check (in PHP) because if(!$result) doesn't work...

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Renegade wrote: What happens if the query is empty... how do I do a check (in PHP) because if(!$result) doesn't work...

If there is no error, then the !result check is useless for testing the returned value:

PHP Manual wrote: For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a resource on success, or FALSE on error.

You will have to analyse the data in the resource to check the number of rows:

PHP Manual wrote:
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement

http://php.inspire.net.nz/manual/en/function.mysql-query.php

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Great, thanks Smiling

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

What does this error mean?

Unable to jump to row 0 on MySQL result index 8
'

Busy's picture

He has: 6,151 posts

Joined: May 2001

Usually your searching for wrong context, like a value as a character

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Ah.. ok, thanks guys Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.