我正在為我的家庭作業(yè)設(shè)置一個(gè)網(wǎng)站,我需要使用 php 從 html 表中的 sql 表中隨機(jī)顯示 1 行。例如,我的網(wǎng)站稱為隨機(jī)數(shù)據(jù),該網(wǎng)站將為您生成隨機(jī)數(shù)據(jù)(除了它實(shí)際所做的只是顯示已插入數(shù)據(jù)庫(kù)中的數(shù)據(jù))。那么,有誰(shuí)知道從 sql 表中顯示 1 行數(shù)據(jù)并在表的每個(gè)單元格中使用 php 將其顯示在 html 表中以從數(shù)據(jù)庫(kù)中檢索數(shù)據(jù)所需的代碼?這是我到目前為止所擁有的(當(dāng)我不嘗試僅隨機(jī)選擇一行時(shí)它有效,但是當(dāng)我選擇所有行時(shí),它確實(shí)有效。請(qǐng)幫助!:)<?php $username = "root"; $password = "root"; $dbname = "fakedeets"; $conn = new mysqli("localhost", $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT FLOOR(RAND()*(10-5+1)+5);"; $result = $conn->query($sql); if($row = $result-> fetch_assoc()) { echo "<tr><td>". $row["firstname"]. "</td><td>". $row["lastname"]. "</td><td>". $row["dob"]. "</td><td>". $row["gender"]. "</td><td>". $row["emailaddress"]. "</td><td>". $row["phonenumber"]. "</td><td>". $row["photo"]. "</td></tr>"; echo "</table>"; } else { echo "0 results"; } $conn->close();?>我希望出現(xiàn)的只是從 sql 數(shù)據(jù)庫(kù)中隨機(jī)選擇的 html 文檔上顯示的 1 行數(shù)據(jù)。相反,它只是什么都不輸出
1 回答

慕俠2389804
TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
你可以使用 SELECT {your columns} FROM {table} ORDER BY RAND() LIMIT 1
這將從您需要的表中選擇列,隨機(jī)排序,然后將集合限制為一個(gè)條目。用您需要的行數(shù)替換 1。
- 1 回答
- 0 關(guān)注
- 247 瀏覽
添加回答
舉報(bào)
0/150
提交
取消