[英]How to make a hyperlink to all the rows in a particular column of a table using php
[英]How to Traverse all rows in a column using php
這是我的表數據和行
users under_userid
------- ------------
demo NULL
user1 demo
user2 user1
user3 user1
user4 user2
我試過如下查詢
$query = mysqli_query($con,"select under_userid from user where users='user4'");
$result = mysqli_fetch_array($query);
foreach ($result as $x => $x_value)
{
echo 'Value = ' . $x_value;
}
我得到了上述語句的輸出
Value = user2
但我需要從輸出值中獲取所有 under_userid
Expected Output
Value = user2
Value = user1
Value = demo
如何從輸出值中獲取所有 under_userid?
那么我需要使用 PHP 正確的 MySQL 查詢和循環語句嗎?
你需要一個遞歸函數:
function get_all_under($con, $userid) {
$stmt = mysqli_prepare($con, "SELECT under_userid FROM user where user = ? AND under_userid IS NOT NULL");
mysqli_stmt_bind_param($stmt, "s", $userid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $under_userid);
$results = array();
while (mysqli_stmt_fetch($stmt)) {
echo "Value = $under_userid<br>";
$results[] = $under_userid;
}
// Now get the next level
foreach ($results as $under_userid) {
get_all_under($con, $user_userid);
}
}
get_all_under("user4");
將您的查詢替換為:
SELECT under_userid FROM user WHERE under_userid != 'NULL'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.