Php bind_result()和fetch()如何更改范围外的变量?

Php bind_result()和fetch()如何更改范围外的变量?,php,oop,mysqli,Php,Oop,Mysqli,根据这个bind_结果应该不能改变我的变量 $query = 'SELECT username, status FROM table WHERE id = ?'; $id = 5; $stmt = $mysqli->prepare($query); $stmt->bind_param('i', $id); $stmt->execute(); $stmt->bind_result($username, $status); $stmt->fetch(); // N

根据这个bind_结果应该不能改变我的变量

$query = 'SELECT username, status FROM table WHERE id = ?';
$id = 5;

$stmt = $mysqli->prepare($query);

$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($username, $status);
$stmt->fetch();

// Now I can use $username and $status

echo "$username has the status of $status";
为什么可能,这是如何工作的,我如何在自己的php类/方法中做到这一点

下面是一个例子:

// Notice the '&' on the 2nd param
// This is being passed in as a reference
function addToVal($val, &$ref){
    // If we edit '$ref', we are are editing the
    // variable being "referenced"
    $ref += $val;
}
那么,如果我们这样做:

$abc = 3;
addToVal(5, $abc);
echo $abc; // 8
$abc
正在由
addToVal
更新,因为它是通过引用传递的

文档中关于绑定结果的注释

bool mysqli_stmt::bind_结果(mixed&$var1[,mixed&$…])

它说,
和$var1
,这是一个参考。

它使用

下面是一个例子:

// Notice the '&' on the 2nd param
// This is being passed in as a reference
function addToVal($val, &$ref){
    // If we edit '$ref', we are are editing the
    // variable being "referenced"
    $ref += $val;
}
那么,如果我们这样做:

$abc = 3;
addToVal(5, $abc);
echo $abc; // 8
$abc
正在由
addToVal
更新,因为它是通过引用传递的

文档中关于绑定结果的注释

bool mysqli_stmt::bind_结果(mixed&$var1[,mixed&$…])

上面写着
和$var1
,这是一个参考。

通过参考-

function setMagically(&$ref){ 

    $ref = "Whatever value"; 

}


setMagically($someVar); 
echo $someVar;
通过引用-

function setMagically(&$ref){ 

    $ref = "Whatever value"; 

}


setMagically($someVar); 
echo $someVar;

谢谢你花时间向我解释这件事!谢谢你花时间向我解释这件事!