简体   繁体   中英

PHP/MySQL - Using JOIN to fetch data from different table

I've been reading a lot of posts on how to fetch data on different tables.

In my users table I have:

user_id int(5)
first_name varchar(30)
last_name varchar(30)
email varchar(30)
password varchar(30)
registration_date date

In my blog_post table I have:

user_id int(5)
title
post
author_id int(5)
date_posted
description

PHP code:

  include('mysql_connect.php');
  $query = "select blog_post.author_id, blog_post.* ,users.user_id FROM blog_post, users where blog_post.user_id = users.user_id";
  $result = @mysql_query($query);
  mysql_close();

?>
<h2>BLOGS</h2>
<?php

  $i=1;
  while ($row = mysql_fetch_array($result))
  {
    echo "<b>#:</b>" . $i . "<br>";
    echo "<b>POST ID:</b>" . $row[0] . "<br>";
    echo "<b>TITLE:</b>" . $row[1] . "<br>";
    echo "<b>DESCRIPTION:</b>" . $row[5] . "<br>";
    echo "<b>DATE POSTED:</b>" . $row[4] . "<br>";
    echo "<b>POST:</b><br>" . $row[2] . "<br>";
    echo "<b>AUTHOR ID:</b><br>" .$row['author_id']. "<br>";
    echo "<br>";
    $i++; 
  }

I have to get the author_id from the one who logs in and post a blog, but I can't do it.

mysql Syntax seems to be ok, but old. You should use / learn about "join" for linking Tables in SQL.

Do you get any Output? Because it seems to me, that

mysql_close();

is wrong in that place. Just remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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