Search from multiple tables ??

Thread Solved

Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Search from multiple tables ??

 
0
  #1
May 8th, 2008
hi,
How can I search from multiple tables in a db?DB I am using is MySql db.

Thank You,
Suhasini
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Search from multiple tables ??

 
0
  #2
May 8th, 2008
SELECT * FROM tablename WHERE somefield LIKE '%searchquery%'

other than taking the time to write an iterative script you will need to write individual SQL statements for each table.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

 
0
  #3
May 8th, 2008
Does this work for something like this..
I have a db in which i have 26 tables suppose say A to Z.Now if i want to search some 's1' in table 'S' will this work?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Search from multiple tables ??

 
0
  #4
May 8th, 2008
then you would search using the following in that circumstance.

  1. SELECT * FROM S WHERE s1 LIKE '%what you want to search for%'

The % sign either side of the search text indicates two wildcards and will search for the string inside other strings.

E.G.
  1. table 'user' has rows...
  2.  
  3. id | name
  4. 1 | 'john'
  5. 2 | 'alan'
  6. 3 | 'joe'

when firing this SQL statement
  1. SELECT id FROM user WHERE name LIKE '%jo%'

it will return rows with ID's 1& 3

because john and joe contains the phrase 'jo'

if you wanted to do this through all your tables and they have a similiar format.

then you could use a php array to hold the table name, ID and search field and loop through generating the sql statement dynamically.
Last edited by Fungus1487; May 8th, 2008 at 12:24 pm. Reason: stupid mistake :D
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

 
0
  #5
May 9th, 2008
Thanks for ur reply.but i want to search a keyword from all the tables at once from a db.
As u said i have tried like tht but am getting the result onli from the last query.
For example:
  1.  
  2. $var = @$_POST['keyword'] ;
  3. $trimmed = trim($var);
  4.  
  5. $query = "select * from a where MedName like \"%$trimmed%\"
  6. order by dispname";
  7.  
  8. $query = "select * from b where MedName like \"%$trimmed%\"
  9. order by dispname";
  10.  
  11. $numresults=mysql_query($query);
  12. $numrows=mysql_num_rows($numresults);

Now if i want to search '1' and i wanted the result shuld be 'a1,b1' but am getting onli 'b1'

so waht shuold i do to get both a1,b1??
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Search from multiple tables ??

 
0
  #6
May 9th, 2008
You can do it this way.
  1. <?php
  2. $con = mysql_connect("localhost","root");
  3. mysql_select_db("test");
  4. //get all the tables from the database test
  5. $q = "show tables";
  6. $r = mysql_query($q);
  7. $searchresult = array();
  8. while($row = mysql_fetch_array($r)) {
  9. $table = $row[0]; // get the tablename
  10. $searchtables = "select * from ".$table." where col1 like '%".$searchstring."%'"; //search the table column for the search string
  11. $result = mysql_query($searchtables);
  12. while($rows = mysql_fetch_array($result)) {
  13. $searchresult[] = $rows['name'];
  14. }
  15. }
  16. // $searchresult will have all the records from all the tables for the searchstring
  17. ?>

Cheers,
Nav
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

 
0
  #7
May 9th, 2008
This is the error am getting after trying this code

  1. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\exphp\medwits\search.php on line 14
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Search from multiple tables ??

 
0
  #8
May 9th, 2008
Print the query and show us what it says..
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

 
0
  #9
May 9th, 2008
same error is coming...by the way which line to print??
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Search from multiple tables ??

 
0
  #10
May 9th, 2008
Have you changed the column and table names to suit your requirements ? Print out $searchtables and tell us what it prints.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC