[英]MySQL query finds rows but returns nothing
我有一個像這樣創建的 2 列表(每個用戶都有一個自己的表):
$sql = "CREATE TABLE $user_name (
driver TINYTEXT NOT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
我已經(手動,使用 phpMyAdmin)將數據插入到這樣的一個表中。 reg_date 是在當前時間設置的,並插入了一些隨機字符串(數字,比如 01234)。
我嘗試訪問這樣的行:
$mysql_qry2 = "SELECT * FROM $username";
$result2 = mysqli_query($conn, $mysql_qry2);
$ans = "";
if(mysqli_num_rows($result2) > 0) {
while ($row = mysqli_fetch_assoc($result2)) {
$ans = $ans . $row["driver"];
$ans = $ans . " ";
$x = $row["driver"];
echo "$x";
echo "a";
}
$ans = $ans . "asd";
echo "$ans";
} else {
echo "b";
}
編輯:修復我錯誤的變量名后,盡管有 3 行數據,但“SELECT *”查詢返回零行。
您檢查了錯誤變量mysqli_num_rows($result) > 0
計數。
$mysql_qry2 = "SELECT * FROM $username";
$result2 = mysqli_query($conn, $mysql_qry2);
$ans = "";
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$ans = $ans . $row["driver"];
echo "$ans";
echo "a";
}
} else {
echo "b";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.