dth42345 2016-10-28 14:53
浏览 47

PHP和MySQL:如何从两个表中获取所有数据库条目?

I have the following code which gets data from a table entry and displays some html for each entry. It works fine but i want it to display html for the entries in that table AND another table. I could just duplicate this code and change it a bit to get the entries in a different table but that creates another problem, all the entries from the second table would start at the end of the entree list from the first table. How can I display the entries from both table in the order of most relevant to what the user types in? Here is my code that just displays entries from the first table in order of when they were inserted into the table. Can someone tell me how to display all the entries from two different tables in order of relavace to a user search query? My code so far, I have not been able to get much further than this.

$pagesQuery  = mysql_query("SELECT count(id) FROM(`posts`) WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%{$terms}%'");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);

$currentname = mysql_query("SELECT * FROM `posts` WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%{$terms}%' LIMIT $start, 5"); 


while ($row = mysql_fetch_array($currentname)) {
    //recieve relevant data.
    $title = $row[0];
    $desc = $row[13];
    $ID = $row[6];
    $views = $row[3];
    $user = $row[7];
    $type = $row[15];
    //fetch the last id from accounts table.
    $fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
    $lastrow1 = mysql_fetch_row($fetchlast1);
    $lastid1 = $lastrow1[6];
    //acquire the username of postee.
    for ($i1=1; $i1 <= $lastid1; $i1++) { 
        $currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
        while ($row1 = mysql_fetch_array($currentname1)) {
            $username1 = $row1[0];
        }
    }

    //Format Title, description and view count.
    $title2 = rtrim($title);
    $donetitle = str_replace(" ", "-", $title2);
    $donetitle1 = str_replace(".", "", $donetitle);
    $donetitle2 = str_replace("-", "-", $donetitle1);
    $donetitle3 = str_replace(":", "-", $donetitle2);
    $url = "articles/".$ID."/".$donetitle3."";

    $donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
    $donedesc = '';

    if(strlen($desc) > 150) {
        $donedesc = explode( "
", wordwrap( $desc, 150));
        $donedesc1 = $donedesc[0] . '...';                          
    } else {
        $donedesc1 = $desc;                         
    }
    $SRCIMG = '';
    $finviews = number_format($views, 0, '.', ',');
    if($type == '1'){
        $SRCIMG = "img/icons/video.png";
    } else {
        $SRCIMG = "img/icons/article.png";
    }
    //Give  results

    if($row[10] == null){
        $SRC = "img/tempsmall.jpg";
    }else{
        $SRC ="generateThumbnailSmall.php?id=$ID"; 
    }
    echo "<div id = \"feature\">
            <img src=\"$SRC\" alt = \"article thumbnail\" />
            <img src=\"$SRCIMG\" alt = \"icon\" id=\"icondisp\"/>
        </div>
        <div id = \"feature2\">
             <a href= \"$url\" id = \"titletext\" alt = \"article title\">$donetitle</a>
             <p id=\"resultuser\" >$username1</p>
             <p id=\"resultp\">$donedesc1</p>
             <a href = \"sendflag.php?title=$title&url=$url&id=$ID&userid=$user\" id = \"flag\" alt = \"flag\"><img src=\"img/icons/flag.png\"/></a><b id=\"resultview\">$finviews views</b> 
        </div>
        <div id = \"border\"></div>";        
}
  • 写回答

4条回答 默认 最新

  • douchi5822 2016-10-28 14:58
    关注

    Why not try union ?

    SELECT * FROM `posts` WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%  {$terms}%' 
    union
    SELECT * FROM `otherposts table` WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%{$terms}%' 
    order by yourRelevanceField
    LIMIT $start, 5"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: