sql >> Database teknologi >  >> RDS >> Mysql

Forvandling af bruger-id til navn (separate tabeller) i PHP

Så hvad du har brug for er et par JOIN s (implicit INNER JOIN ) mod users , en til at deltage i følgeren og en til at deltage i den fulgte.

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

I din sløjfe er navnene tilgængelige i 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. Tilføj tidsstempelkolonne med standard NOW() kun for nye rækker

  2. Sådan indstilles mandag som første dag i ugen i SQL Server

  3. undgå MySQL-injektioner med Zend_Db-klassen

  4. SQL Server-strengfunktioner (fuld liste)