User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 428,201 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,224 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1199 | Replies: 24 | Solved
Reply
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Search from multiple tables ??

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 37
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Search from multiple tables ??

  #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  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

  #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  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 37
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Search from multiple tables ??

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

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.
table 'user' has rows...

id | name
1  | 'john'
2  | 'alan'
3  | 'joe'

when firing this SQL statement
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 11:24 am. 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  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

  #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:
 $var = @$_POST['keyword'] ;
  $trimmed = trim($var);

$query = "select * from a where MedName like \"%$trimmed%\"  
  order by dispname"; 

$query = "select * from b where MedName like \"%$trimmed%\"  
  order by dispname";
  
 $numresults=mysql_query($query);
 $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  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Search from multiple tables ??

  #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
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

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

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  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Search from multiple tables ??

  #8  
May 9th, 2008
Print the query and show us what it says..
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Search from multiple tables ??

  #9  
May 9th, 2008
same error is coming...by the way which line to print??
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Search from multiple tables ??

  #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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC