我試圖在查詢中添加默認圖像,如果該文件不存在。 當查詢沒有while循環時,我成功了。但因爲我需要循環這個值。我想將默認圖像添加到查詢中。如果文件不存在,則爲默認圖像mysql pdo
我不會得到任何錯誤,它只是從我的數據庫打印出一些隨機信息。
function fetch_tweets($uid){
$uid = (int)$uid;
$query = $this->link->query
("SELECT user.id, user.email, user.username, tweets.message, tweets.date,
userdetails.profile_img, userdetails.firstname, userdetails.lastname,
following.id, following.user_id, following.follow_id
FROM user
LEFT JOIN
following ON user.id = following.user_id
JOIN userdetails ON user.id = userdetails.user_id
JOIN tweets ON userdetails.user_id = tweets.user_id
WHERE user.id='{$uid}'
OR
user.id IN (SELECT follow_id
FROM following
WHERE
following.user_id = '{$uid}')
GROUP BY tweets.date ORDER BY tweets.date DESC "
);
$tweet = array();
while(($row = $query->fetch(PDO::FETCH_ASSOC)) !==FALSE) {
$tweet[] = $row;
$tweet['profile_img'] = (file_exists("img/{$uid}.jpg")) ?
"img/{$uid}.jpg" : "img/default.jpg" ;
}
return $tweet;
}
PDO與文件無關。它是一個數據庫驅動程序。 – 2013-04-25 07:59:41