[英]How do I make sure the rapidly changing data from my MySQL DB is accurately represented in php scripts?
[英]How do I make a clickable url in my php page after getting data from MySQL DB?
我正在嘗試從數據庫的1列中提取數據,並將其放入從另一列中提取URL的鏈接中。 不幸的是,我無法做到這一點。 我已經附上了輸出的屏幕截圖,這是我正在使用的代碼:
$sql = "SELECT DISTINCT item_title, item_url, item_date, item_author FROM nbth";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
$HTML .= "<a href=".$row['item_url'].">".$row['item_title']."</a>";
echo $HTML;
}
} else {
echo "0 results";
}
結果的屏幕截圖:[![在此處輸入圖片描述] [1]] [1]
標題確實成為鏈接,但是get一次又一次地循環,第一行中的第一個標題,然后是第一個標題和第二個,然后是第一個,第二個和第三個,依此類推。 因此,如何解決此循環問題並仍將標題顯示為鏈接。
$HTML .= "<a href=".$row['item_url'].">".$row['item_title']."</a>";
刪除等號前的句點(字符串concat)運算符:
$HTML = "<a href=".$row['item_url'].">".$row['item_title']."</a>";
它導致循環的每次迭代都連接"<a href=".$row['item_url'].">".$row['item_title']."</a>";
每次都移至$HTML
變量。
另外,請用引號( "
)括住href屬性的值。
在循環外回顯$ html
要么
while($row = $result->fetch_assoc()) {
$HTML ='';
echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
$HTML = "<a href=".$row['item_url'].">".$row['item_title']."</a>";
echo $HTML;
}
} else {
echo "0 results";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.