•
•
•
•
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
![]() |
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.
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
then you would search using the following in that circumstance.
The % sign either side of the search text indicates two wildcards and will search for the string inside other strings.
E.G.
when firing this SQL statement
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.
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
•
•
Join Date: May 2008
Posts: 28
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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??
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??
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation:
Rep Power: 6
Solved Threads: 161
You can do it this way.
Cheers,
Nav
php Syntax (Toggle Plain Text)
<?php $con = mysql_connect("localhost","root"); mysql_select_db("test"); //get all the tables from the database test $q = "show tables"; $r = mysql_query($q); $searchresult = array(); while($row = mysql_fetch_array($r)) { $table = $row[0]; // get the tablename $searchtables = "select * from ".$table." where col1 like '%".$searchstring."%'"; //search the table column for the search string $result = mysql_query($searchtables); while($rows = mysql_fetch_array($result)) { $searchresult[] = $rows['name']; } } // $searchresult will have all the records from all the tables for the searchstring ?>
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*
*PM asking for help will be ignored*
•
•
Join Date: May 2008
Posts: 28
Reputation:
Rep Power: 1
Solved Threads: 0
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
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation:
Rep Power: 6
Solved Threads: 161
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation:
Rep Power: 6
Solved Threads: 161
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*
*PM asking for help will be ignored*
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
•
•
•
•
adsense adult advertising blogging book business coding copyright css cult of the dead cow data development div engine europe forum google hacking html internet search legal malware marketing mcafee microsoft monetization msn news pagerank privacy publishing revenue search search engine security seo sex silverlight software spyware support symantec tables technical universe video web webmaster wiki yahoo
- Searching Multiple tables... Please help me!!!!! (PHP)
- Query multiple tables with duplicate data (MySQL)
- Searching for a record in multiple tables (VB.NET)
- URGENT: Implementing search with multiple dissimilar MySQL tables (MySQL)
- Query multiple tables? (MySQL)
- updating 2 HTML tables on one PHP page (PHP)
Other Threads in the PHP Forum
- Previous Thread: Initialization of an Array
- Next Thread: Carrying Session via URL



Linear Mode