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 330,335 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 2,744 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: 320 | Replies: 24 | Solved
Reply
Join Date: May 2008
Posts: 28
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Light Poster

Search from multiple tables ??

  #1  
5 Days Ago
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: 344
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 28
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Search from multiple tables ??

  #2  
5 Days Ago
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
Posts: 28
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Light Poster

Re: Search from multiple tables ??

  #3  
5 Days Ago
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: 344
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 28
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Search from multiple tables ??

  #4  
5 Days Ago
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 : 5 Days Ago 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
Posts: 28
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Light Poster

Re: Search from multiple tables ??

  #5  
4 Days Ago
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: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 161
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Search from multiple tables ??

  #6  
4 Days Ago
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
Posts: 28
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Light Poster

Re: Search from multiple tables ??

  #7  
4 Days Ago
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: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 161
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Search from multiple tables ??

  #8  
4 Days Ago
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
Posts: 28
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Light Poster

Re: Search from multiple tables ??

  #9  
4 Days Ago
same error is coming...by the way which line to print??
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 161
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Search from multiple tables ??

  #10  
4 Days Ago
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.

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

 

DaniWeb Marketplace (Sponsored Links)
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

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