所以,我一直在编写一些PHP,我从数据库中获取一些数据进行输出。我正常使用函数,但由于某些原因,函数中被拒绝的变量显然是未声明的?
这是我的代码:
function fetchUserData($localUser_id) {
include_once("../../../conConfig/connectToDatabase.php");
$dataQuery = mysqli_query($dbConnect, "SELECT users.emailAddress, userInfo.fullName, userInfo.initials, userInfo.maxWorkspaces FROM users INNER JOIN userInfo ON userInfo.ID = users.ID WHERE users.ID = ".$localUser_id."");
$queryResult = mysqli_fetch_array($dataQuery, MYSQLI_ASSOC);
$db_dash_userName = $queryResult["fullName"];
$db_dash_userEmail = $queryResult["emailAddress"];
$db_dash_userIcon = "initials";
$db_dash_userMaxW = $queryResult["maxWorkspaces"];
$db_dash_userInit = $queryResult["initials"];
echo $db_dash_userName.$db_dash_userEmail.$db_dash_userIcon.$db_dash_userMaxW.$db_dash_userInit."<br><br><br>";
}
//$localUser_id = $_SESSION["user_id"];
$localUser_id = 1;
fetchUserData($localUser_id);
echo $db_dash_userName.$db_dash_userEmail.$db_dash_userIcon.$db_dash_userMaxW.$db_dash_userInit;
它可能有什么问题?它返回一个值,由函数内部的回声输出,但它们不会被最终的回声输出。
是的,我已经尝试过返回variable.variable ......&#39;,但仍然无法解决问题。
提前致谢。
答案 0 :(得分:2)
您需要从函数返回值。 echoing不返回值。您还需要将返回的值分配给变量并使用它来显示输出。
function fetchUserData($localUser_id) {
include_once("../../../conConfig/connectToDatabase.php");
$dataQuery = mysqli_query($dbConnect, "SELECT users.emailAddress, userInfo.fullName, userInfo.initials, userInfo.maxWorkspaces FROM users INNER JOIN userInfo ON userInfo.ID = users.ID WHERE users.ID = ".$localUser_id."");
$queryResult = mysqli_fetch_array($dataQuery, MYSQLI_ASSOC);
$db_dash_userName = $queryResult["fullName"];
$db_dash_userEmail = $queryResult["emailAddress"];
$db_dash_userIcon = "initials";
$db_dash_userMaxW = $queryResult["maxWorkspaces"];
$db_dash_userInit = $queryResult["initials"];
return $db_dash_userName.$db_dash_userEmail.$db_dash_userIcon.$db_dash_userMaxW.$db_dash_userInit."<br><br><br>";
}
//$localUser_id = $_SESSION["user_id"];
$localUser_id = 1;
$output = fetchUserData($localUser_id);
echo $output;