如何使用连接表中的选定项?

时间:2017-01-17 07:43:29

标签: php mysql

如何在变量中存储以下语句的s.Usernamer.Username

$stmt = $this->db->prepare('SELECT 
                                  s.Username AS Username1, 
                                  r.Username AS Username2, 
                                  c.time, 
                                  c.message 
                            FROM chatlog c
                            JOIN user s ON (c.source = s.ID)
                            JOIN user r ON (c.receiver = r.ID)
                            WHERE s.ID = :source AND r.ID = :receiver
                                        ');
$stmt->execute(array(':source' => $sender, ':receiver' => $buddyID ));

// I want to store the Source and the Username here..
$row =  $stmt->fetch(PDO::FETCH_ASSOC);
$chatID = $row['Username1'].$row['r.Username2'];

// ..to create a unique logfile here ..
$dummy = fopen($chatID.".html",'w');
fclose($dummy);

// .. and store the log finally in that file which is used to print in the chatbox
while($log = $stmt->fetch(PDO::FETCH_ASSOC))
{
$fp = fopen($chatID.".html", "a");
fwrite($fp, "<div class='chat_msg'>(" . $log['time'] . ")" . $log['name'] .":  " . $log['message'] . "<br />");
fclose($fp);
}

1 个答案:

答案 0 :(得分:0)

你的意思是?

SELECT 
       s.Username AS Username1, 
       r.Username AS Username2, 
       c.time, 
       c.message 
FROM chatlog c
JOIN user s ON (c.source = s.ID)
JOIN user r ON (c.receiver = r.ID)
WHERE s.ID = :source AND r.ID = :receiver