Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Mengubah ID pengguna menjadi nama (tabel terpisah) di PHP

Jadi yang Anda butuhkan adalah sepasang JOIN s (secara implisit INNER JOIN ) terhadap users , satu untuk bergabung di pengikut dan satu untuk bergabung di diikuti.

SELECT 
  /* Rather than SELECT * you need to be specific about the columns, and
     give them aliases like followed_name since you have 2 tables with the same
     column names in the query */
  ufollower.id AS follower_id,
  ufollower.username AS follower_name,
  ufollowed.id AS followed_id,
  ufollowed.username AS followed_name
FROM
  /* JOIN twice against users, once to get the follower and once to get the followed */
  user_follow 
  /* users aliased as ufollower to get the follower details */
  JOIN users ufollower ON ufollower.id = user_follow.follower
  /* users aliased as ufollowed to get the followed details */
  JOIN users ufollowed ON ufollowed.id = user_follow.followed
WHERE
  user_follow.follower = $p_id

Di loop Anda, nama tersedia di follower_name, followed_name .

while($apple = mysql_fetch_array($following)){
   // Be sure to wrap the name in htmlspecialchars() to encode characters that could break html.
   // This has the followed id in the href and the followed name in the link text...
   echo '<a href="'.$apple['followed_id'].'">+'.htmlspecialchars($apple['followed_name']) .'</a> ';
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara Membuat API GraphQL Tanpa Server untuk MySQL, Postgres, dan Aurora

  2. Bagaimana cara menyimpan tag dalam tag MySQL, total satu bidang atau satu bidang untuk setiap tag?

  3. MAX() vs GREATEST() di MySQL:Apa Bedanya?

  4. Cara Mengakses PhpMyAdmin tanpa login cPanel

  5. Laravel:Data string, terpotong kanan:1406 Data terlalu panjang untuk kolom