简体   繁体   English

我如何在函数外部访问此变量

[英]How do I access this Variable outside the function

function GetActiveWeek($mysqli, $ActiveWeekEndingDate) {
    $stmt = "SELECT ParameterValue FROM INF_WebConfig WHERE ParameterName = 'ActiveWeekEndingDate'";
    $ActiveWeekEndingDate = $mysqli->query($stmt);
    return $ActiveWeekEndingDate;
}

I need to echo $ActiveWeekEndingDate elsewhere how do I access this outside of the function correctly. 我需要在其他地方回显$ ActiveWeekEndingDate,如何在函数外部正确访问它。

EDIT: 编辑:

I managed to get it working with the Following but it isn't really nessicary to prepare the statement is it? 我设法使它与“跟随”一起使用,但是准备声明不是很必要吗?

function GetActiveWeek($mysqli) {
    $stmt = $mysqli->prepare("SELECT ParameterValue FROM INF_WebConfig WHERE ParameterName = 'ActiveWeekEndingDate'"); 
    $stmt->execute();
    $stmt -> bind_result($ActiveWeekEndingDate);
    $stmt -> fetch();
    $stmt -> close();
    $mysqli -> close();
    return $ActiveWeekEndingDate;
}

Why not handle it in the response? 为什么不在响应中处理呢?

$ActiveWeekEndingDate = GetActiveWeek();
echo $ActiveWeekEndingDate;

If you insist on having it global (discouraged), add this above the function: 如果您坚持要全局使用(不推荐使用),请在函数上方添加以下代码:

global $ActiveWeekEndingDate;

You need to assign the return value from the GetActiveWeek function to a variable you can access out of the function. 您需要将GetActiveWeek函数的返回值分配给可以从该函数访问的变量。

function GetActiveWeek($mysqli, $ActiveWeekEndingDate) {
        $stmt = "SELECT ParameterValue FROM INF_WebConfig WHERE ParameterName = 'ActiveWeekEndingDate'";
        $ActiveWeekEndingDate = $mysqli->query($stmt);
        return $ActiveWeekEndingDate;
}

$retVal = GetActiveWeek($mysqli, $ActiveWeekEndingDate);
function GetActiveWeek($mysqli) {
    $stmt = $mysqli->prepare("SELECT ParameterValue FROM INF_WebConfig WHERE ParameterName = 'ActiveWeekEndingDate'"); 
    $stmt->execute();
    $stmt -> bind_result($ActiveWeekEndingDate);
    $stmt -> fetch();
    $stmt -> close();
    $mysqli -> close();
    return $ActiveWeekEndingDate;
}

This was how i achieved the result. 这就是我取得结果的方式。 I just call the function where the variable is initialized. 我只是调用初始化变量的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM