<?php
$result = mysqli_query($conn,"SELECT * FROM news ORDER BY date DESC
LIMIT 5")or die(mysql_error());
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
echo "<table align='left' CELLPADDING='50' >";
while($row = mysqli_fetch_array($result))
{
if ($row['photoid'] == NULL){
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" .$row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
}
else
{
$result = mysqli_query($conn,"SELECT news.title,news.date,news.news,photo.photo FROM news, photo WHERE news.photoid = photo.photoid ORDER BY date DESC") or die(mysql_error());
$row = mysqli_fetch_array($result);
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" . $row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
echo "<td style='width: 800px'>" . '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode($row["photo"]).'" >' . "</td>";
}
}
echo "</table>";
mysqli_close($conn);
?>
因此,網站顯示新聞欄,代碼從數據庫中獲取新聞數據,如果有照片(如果新聞有照片去),然後它添加消息與桌上的照片。如果沒有照片將消息添加到沒有人的表格中。簡單。目前,用於測試我有兩個新聞文章都與照片,第一個完美的作品,則第二錯誤未定義的索引爲mysql結果
Undefined index: photoid in
對於線if ($row['photoid'] == NULL){
。有一張照片。
所以它不存在 – 2014-11-03 16:06:35
你確定你有你'news'表中的列稱爲'photoid'你不要在您的查詢選擇該列? – vaso123 2014-11-03 16:08:21
您的數據庫中沒有列名'photoid' – 2014-11-03 16:08:24