| | |
Search from multiple tables ??
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread 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.
PHP Syntax (Toggle Plain Text)
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.
PHP Syntax (Toggle Plain Text)
table 'user' has rows... id | name 1 | 'john' 2 | 'alan' 3 | 'joe'
when firing this SQL statement
PHP Syntax (Toggle Plain Text)
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
•
•
Join Date: May 2008
Posts: 90
Reputation:
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:
PHP Syntax (Toggle Plain Text)
$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??
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
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
This is the error am getting after trying this code
PHP Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- 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: question of the day
- Next Thread: Overloading PHP Functions
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube






