简体   繁体   English

PHP MYSQL多表查询

[英]PHP MYSQL Multiple table query

I have two tables 'p_tuts' and 'h_tuts'. 我有两个表“ p_tuts”和“ h_tuts”。 I'm using a method to display all the returned rows. 我正在使用一种方法来显示所有返回的行。 Although I'm not sure how to set it up to run multiple queries, I can only get it to return one query at a time. 尽管我不确定如何设置它以运行多个查询,但是我只能让它一次返回一个查询。 Heres my code... 这是我的代码...

   public function QueryAll($my_field, $limit) {

        $query = mysql_query("SELECT * from $my_field LIMIT $limit");
        $rows = array();

    while($row = mysql_fetch_array($query)) {
        $rows[] = $row;

    }

  return $rows;   }

Here is my index file 这是我的索引文件

 <?php $results = $Database->QueryAll('p_tuts', 5); ?>       
    <?php foreach ($results as $result): ?>
           <div class="tutorial">
                <div class="tut_header"><h2><a href="view_article_p.php?id=<?php echo $result['id']; ?>" title="<?php echo $result['tut_header']; ?>"><?php echo $result['tut_header']; ?></a></h2></div>
                <div class="tut_poster">Posted by: <a href="#" title="Adam"><?php echo $result['tut_poster']; ?></a> on <?php echo $result['tut_date']; ?></div>
                <div class="tut_img rounded"><img src="<?php echo "img_uploads/". $result['tut_img']; ?>" width="180" height="151" /></div>
                <div class="tut_content"><p><?php $Website->CharLimit($result['tut_content'], 800); ?></p></div>   
          </div>
            <?php endforeach; ?>

I'd really like to adapt it so I can use this class to display both multiple tables, rather than just the one. 我真的很想适应它,以便可以使用此类显示两个表,而不仅仅是一个表。

kind regards 亲切的问候

如果不知道两个表之间的关系,很难回答这个问题,但是看起来您需要使用JOINUNION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM