在函数内PHP查询不起作用

时间:2017-02-11 23:39:32

标签: php function mysqli scope

这对我没有意义吗?..

好的,所以我有一个我正在做的SQL查询

$wut = new mysqli("1", "2", "3", "4");
$updateReferralPoints = $wut->prepare("UPDATE `referrals` SET `totalreferrals` = ? WHERE `referralkey` = ?");
$updateReferralPoints->bind_param("ss", $referralPointAmount,$referralKeyForUpdate);
$referralPointAmount = 10;
$referralKeyForUpdate = "1234";
$updateReferralPoints->execute();

完全正常......

BUT

当我将它添加到函数中时,SQL查询只是不起作用..

所以例如这不起作用

if($doUpdate)
{
    updateReferralPointsCall();//DOESN'T WORK?
}

function updateReferralPointsCall()
{
    $wut = new mysqli("1", "2", "3", "4");
    $updateReferralPoints = $wut->prepare("UPDATE `referrals` SET `totalreferrals` = ? WHERE `referralkey` = ?");
    $updateReferralPoints->bind_param("ss", $referralPointAmount,$referralKeyForUpdate);
    $referralPointAmount = 10;
    $referralKeyForUpdate = "1234";
    $updateReferralPoints->execute();
}

但这会起作用

if($doUpdate)
{
    $wut = new mysqli("1", "2", "3", "4");
    $updateReferralPoints = $wut->prepare("UPDATE `referrals` SET `totalreferrals` = ? WHERE `referralkey` = ?");
    $updateReferralPoints->bind_param("ss", $referralPointAmount,$referralKeyForUpdate);
    $referralPointAmount = 10;
    $referralKeyForUpdate = "1234";
    $updateReferralPoints->execute();
}

1 个答案:

答案 0 :(得分:0)

您需要将mysqli对象传递给函数。你只能在像这样的普通函数中调用全局变量。

所以,这样的事情应该有效:

if($doUpdate)
{
    updateReferralPoints($mysqlconnection);//DOESN'T WORK?
}

function updateReferralPoints($updateReferralPoints)
{