return不会从while循环中检索所有数据

时间:2015-11-28 14:42:03

标签: php function mysqli return echo

所以我很难理解为什么,如果我在函数结束时使用return,它不返回所有值,只返回一个。 但是如果我使用echo,则会显示所有值。 功能新手,并试图更多地理解和利用它们。非常感谢任何解释。

function find_all_users($Teacher_role) {
global $mysqli;
if ($Teacher_role == "teacher") {
    $set_update_role = "teacher";
}
$set_visable = 1;
if (!($stmt = $mysqli->prepare("SELECT user_id, user_email, first_name, last_name, role FROM users WHERE role = ? AND visible = ? ORDER BY user_id ASC"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
/* 2 Bind params */
if (!$stmt->bind_param("si", $Teacher_role, $set_visable)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
/* 1 Execute statements */
if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$result = $stmt->get_result();
// output data of each row

while ($row = mysqli_fetch_assoc($result)) {
    $user_id = $row["user_id"];
    $user_email = $row["user_email"];
    $user_Fname = $row["first_name"];
    $user_Lname = $row["last_name"];
    $user_role = $row["role"];

    $output = "<tr>";
    $output .= "<td>" . $user_id . "</td>";
    $output .= "<td>" . $user_email . "</td>";
    $output .= "<td>" . $user_Fname . "</td>";
    $output .= "<td>" . $user_Lname . "</td>";
    $output .= "<td>" . $user_role . "</td>";
    $output .= "<td> <a href='update_" . $set_update_role . ".php?id=" . $user_id . "'>Edit</a>" . "</td>";
    $output .= "</tr>";
    return $output;
}

mysqli_close($mysqli);}

1 个答案:

答案 0 :(得分:0)

不要在while循环中返回$ output。首次运行循环时会立即返回$ output值。让while循环为所有值构建$ output,并且只有在完成所有操作后才返回$ output。

private:
int _id[9];
string _name;
int _age;