【问题标题】:PHP Query Not Working When Within Function [duplicate]PHP查询在函数内不起作用[重复]
【发布时间】:2017-06-30 04:02:47
【问题描述】:

这对我来说没有意义吗?..

好的,我有一个 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();

一切正常……

但是

当我将它添加到函数时,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();
}

【问题讨论】:

    标签: php function mysqli scope


    【解决方案1】:

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

    所以,这样的事情应该可以工作:

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

    【讨论】:

    • 我不认为这是范围......在提供的示例中,连接是在函数内部创建的,这意味着它应该可以使用。
    • 您也在绑定后定义变量。那是行不通的。
    • 你也应该按索引绑定
    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2017-09-14
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多