简体   繁体   English

从PHP脚本执行时,MySQL临时变量不起作用

[英]MySQL temporary variable does not work when executed from PHP script

When executing the following query in MySQL the rows are updated correctly. 在MySQL中执行以下查询时,行将正确更新。

UPDATE market SET price = 
CASE 
WHEN 
    @order_price:=(
        @market_price:=(IF(
            market_id = 0, 
            (SELECT value FROM fiat WHERE id = 6), 
            (SELECT value FROM fiat WHERE id = 5)
        ) 
        + 
        (
            (order_percentage_dynamic/100) * @market_price
        ))
    ) < IFNULL(exchange_rate_max, 999999999) AND @market_price > IFNULL(exchange_rate_min, -999999999)
THEN @market_price 
WHEN @order_price > exchange_rate_max AND exchange_rate_max IS NOT NULL THEN exchange_rate_max
WHEN @order_price < exchange_rate_min AND exchange_rate_min IS NOT NULL THEN exchange_rate_min 
END 
WHERE bdynamicorder = true;

Executing that same query in a PHP script updates all rows but with NULL value. 在PHP脚本中执行相同的查询将更新所有行,但其值为NULL。

if ($update_stmt = $mysqli->prepare($query)) {

    $update_stmt->execute();

}

Any ideas? 有任何想法吗?

Thanks! 谢谢!

I'm pretty sure that the problem is the lack of initialization of the variables. 我很确定问题是缺少变量的初始化。 Consider this expression: 考虑以下表达式:

@market_price := (IF(market_id = 0, 
                     (SELECT value FROM fiat WHERE id = 6), 
                     (SELECT value FROM fiat WHERE id = 5)
                    ) + 
                    (order_percentage_dynamic/100) * @market_price
                 )

If @market_price has not been set, then this will return NULL -- when either argument to an arithmetic operator is NULL , the result is NULL . 如果@market_price尚未设置,那么这将返回NULL -当任一参数,以算术运算符是NULL ,则结果为NULL

One solution uses additional logic such as coalesce() : 一种解决方案使用其他逻辑,例如coalesce()

@market_price := IF(market_id = 0, 
                    (SELECT value FROM fiat WHERE id = 6), 
                    (SELECT value FROM fiat WHERE id = 5)
                   ) + 
                   (order_percentage_dynamic/100) * coalesce(@market_price, 0);

Alternatively, you can initialize the values using a join : 另外,您可以使用join初始化值:

UPDATE market CROSS JOIN
       (select @market_price := 0, @order_price := 0) vars
    SET price =  . . .

My guess is that when you run this on the database, the variables have already been set to non-NULL values. 我的猜测是,当您在数据库上运行此命令时,变量已设置为非NULL值。

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

相关问题 通过命令行执行时,PHP脚本无法正常工作,但从浏览器执行时,PHP脚本可以正常工作 - PHP script wont work when executed via command line but work fine when executed from browser 通过浏览器执行时,用于发送邮件的php脚本无法正常工作,但从命令行执行时,可以正常工作 - php script for sending mail wont work when executed via browser but work fine when executed from command line 当由PHP执行时,PowerShell脚本不起作用 - PowerShell script doesn't work when executed by PHP 从PHP脚本调用时,CasperJS在Cron中不起作用 - CasperJS does not work in Cron when called from PHP script 将 PHP POST 请求中的值连接到要执行的 python 脚本中的变量 - Connecting values from PHP POST request to a variable in a python script to be executed 通过php执行时,perl脚本无法完全运行 - perl script does not fully run when executed via php 当由Shell脚本执行时,PHP的行为如何? - How does PHP act when executed by a shell script? 当变量包含函数时,为什么它会自动执行PHP - When a variable holds a function, why does it get automatically executed PHP 从PHP触发bash脚本时,服务未执行 - service not getting executed when bash script is triggered from php 提交给自己时防止执行PHP脚本 - Prevent PHP script from being executed when submitting to self
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM