如何选择多少有表的查询

时间:2015-03-24 07:01:01

标签: php mysql sql

我有114个表名 sura1 sura2 .... sura114

现在我想要为搜索引擎选择查询但错误选择

$run = mysqli_query($mysqli,
          "SELECT * FROM ( 
            SELECT * FROM   sura1 
            UNION ALL SELECT * FROM   sura2
           ) WHERE $construct"
          );

...

$button = $_GET ['submit'];
$search = $_GET ['search']; 

if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";

    define("HOST", "localhost");
    define("USER", "root"); 
    define("PASSWORD", "");
    define("DATABASE", "holygod");

    $mysqli = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
    $mysqli->set_charset("utf8");

$search_exploded = explode (" ", $search);

$x = "";
$construct = "";  

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="en_arberry LIKE '%$search_each%'";
else
$construct .="AND en_arberry LIKE '%$search_each%'";

}


$run = mysqli_query($mysqli,"SELECT * FROM ( SELECT * FROM   sura1 UNION ALL SELECT * FROM   sura2) WHERE $construct");

$foundnum = mysqli_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{ 

echo "$foundnum results found !<p>";

$per_page = 6;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;


//$getquery = mysqli_query($mysqli,"SELECT * FROM sura2 WHERE $construct LIMIT $start, $per_page");

$getquery = mysqli_query($mysqli,"SELECT * FROM ( SELECT * FROM   sura1 UNION ALL SELECT * FROM   sura2) WHERE $construct LIMIT $start, $per_page");

while($runrows = mysqli_fetch_assoc($getquery))
{
$sura_num = $runrows ['sura_num'];
$en_arberry = $runrows ['en_arberry'];
$sura_name_fa = $runrows ['sura_name_fa'];

echo "
<a href='$sura_name_fa'><b>$sura_num</b></a><br>
$en_arberry<br>
<a href='$sura_name_fa'>$sura_name_fa</a><p>
";

}

//Pagination Starts
echo "<center>";

$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $max_pages - 1;

if($max_pages > 1)
{   
//previous button
if (!($start<=0)) 
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    

//pages 
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;   
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;                 
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))        
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
} 
$i = $i + $per_page;                                       
}

}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                 
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}

}
//close to end; only hide early pages
else
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
} 
$i = $i + $per_page;              
}
}
}

//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
}   
echo "</center>";
} 
} 

1 个答案:

答案 0 :(得分:1)

我强烈建议您研究不同的数据库分区方法。 因为我假设表格具有相同的设计。 然后,不需要创建连接或跟踪要使用的表。

http://en.wikipedia.org/wiki/Partition_%28database%29 http://dev.mysql.com/doc/refman/5.6/en/partitioning.html