...

View Full Version : Search multiple tables



kasper1339
11-21-2010, 01:50 PM
On my page I've got a search feature, where the user can search through the database. In mysql i've got different tables with the contents I want the search to go through. So I need to search through 2 queries. This is how I've done it.

<?


$records_per_page = 10;


//-query the database table
$sql1="select * from fagudtryk_muskler where dansk like \"$trimmed%\" order by dansk";
$sql2="select * from fagudtryk_led where dansk like \"$trimmed%\" order by dansk";


$result = array("$sql1", "$sql2");
foreach($result as $value)

//-run the query against the mysql query function
$numro=mysql_query($value);

//-count results
$numrows=mysql_num_rows($numro);

$num_pages = ceil($numrows/$records_per_page);
//Determine the page to display
$current_page = (isset($_GET['pg']))?$_GET['pg']:1;
if($current_page<1 || $current_page>$num_pages) { $current_page = 1; }
//Define the limit start position for the current page of records (for current filter)
$limitstart = (($current_page-1)*$records_per_page);


// Build SQL Query
$query = "select * from fagudtryk_muskler where dansk like \"$trimmed%\" order by dansk LIMIT $limitstart, $records_per_page "; // EDIT HERE and specify your table and field names for the SQL query
$query1 = "select * from fagudtryk_led where dansk like \"$trimmed%\" order by dansk LIMIT $limitstart, $records_per_page "; // EDIT HERE and specify your table and field names for the SQL query

$numresults = array("$query", "$query1");
foreach($numresults as $value1)
$value2=mysql_query($value1);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{

echo "<p>Din søgning: &quot;" . $trimmed . "&quot; retunerede ingen resultater</p>";

}


// get results

if ($numrows > 0)
{
// display what the person searched for

echo "<p>Resultat for: &quot;" . $var . "&quot; </p>";



// now you can display the results returned
while($row=mysql_fetch_array($value2)){
$dansk =$row['dansk'];

$ID =$row['id'];


//-display the result of the array

echo "<li>" . "<a href='javascript:void()' onclick='window.location.href=\"search.php?id=" . ($ID) . "&dansk=". ($dansk) . "\"; parent.scrollTo(0,0); return false'><p> " .$dansk . " </a></li>\n</p>";




}}





The problem is that it only uses the last query, so I don't get any results from the first query. What is wrong?

abduraooft
11-22-2010, 07:30 AM
Look in to UNION (http://dev.mysql.com/doc/refman/5.0/en/union.html)

kasper1339
11-22-2010, 07:58 PM
Think I should use JOIN. Tried to do it like this
$sql1="SELECT * FROM fagudtryk_knogler JOIN fagudtryk_muskler ON fagudtryk_knogler.latinsk = fagudtryk_muskler.latinsk where fagudtryk_knogler.latinsk = \"$trimmed%\" or fagudtryk_muskler.latinsk = \"$trimmed%\"
";

But I can't get any results. Whatever I search for it just says 'No results' even though I know there should be a result



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum