简体   繁体   English

使用fetch_object()从MySQL显示另一个相关表中的行

[英]Display Row from Another Related Table from MySQL Using fetch_object()

Trying to echo the author column from a another table. 试图从另一个表中回显作者专栏。 Currently have two tables in database one is called entries and the other comments . 当前数据库中有两个表,一个称为entries ,另一个称为comments Both have IDs but both have the same column name "author". 两者都有ID,但都具有相同的列名“作者”。 In the SQL statement you would do select * from $table1 t1, $table2 t2 where t1.blogID = t2.blogID and t1.blogID = $postID . 在SQL语句中,您可以select * from $table1 t1, $table2 t2 where t1.blogID = t2.blogID and t1.blogID = $postID I know how to echo from just one table using fetch_object() 我知道如何使用fetch_object()从一个表中fetch_object()

if (!empty($postID)) {
$command = "select * from $table_name where blogID = $postID";
$result = $db->query($command);
while ($data = $result->fetch_object()) {
  $postID = $data->blogID;
  echo "<TR><TD>".$postID."</TD>";
  echo "<TD>".$data->author."</TD>";
  echo "<TD>".$data->date."</TD>";
  echo "<TD>".$data->entry."</TD></TR>\n";

But, then, if I want the $command to select on both tables, how would I echo let say t1.author ? 但是,然后,如果我希望在两个表上都选择$command ,我将如何回声让t1.author Meaning do I do $data->t1.author ? 意思是我做$data->t1.author吗? Does that make sense? 那有意义吗? Or what do you guys suggest using the format I've set-up above? 或是您对我上面设定的格式有何建议?

$command="select t1.name as t1name , t2.name as t2name from t1tabla as t1 ,t2table as t2";
$result = $db->query($command);
while ($data = $result->fetch_object()) {

 echo $data->t1name .'<br>';
 echo $data->t2name.'<br>' 
}

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

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