php mysql in变量,PHP查询中的MYSQL变量

The solution to this problem might be a simple over sight of mine.

I am trying to run a MYSQL query stored as a string in PHP. The query runs fine using DBM tool like Navicat but returns false in my PHP development enviorment. Is there something I've over looked?

SET @running_sum = 0;

SELECT

TID,

SumTotal,

T.`Freight`,

T.`Insurance`,

T.`Discount`,

CONCAT(

'$',

FORMAT(

@running_sum :=@running_sum + SumTotal + T.`Freight` + T.`Insurance` - T.`Discount`,

2

)

) AS 'Running Total'

FROM

(

SELECT

TID,

SUM(Quantity * UnitNetValue) AS SumTotal,

T.`Freight`,

T.`Insurance`,

T.`Discount`

FROM

Transactions T

JOIN `Transactions_Products` P ON T.TransactionID = P.TID

WHERE

(

T.TemplateName = ''

OR T.TemplateName IS NULL

)

AND T. STATUS = 1

GROUP BY

TransactionID

) AS T;

I am executing the query like this;

$result = mysql_query($this->query);

$this->query is a string which holds the above query, as it is displayed to you above.

解决方案

The problem is mysql_query() doesn't support multiple queries. Your SET @running_sum = 0; is considered a separate query and so you'll have to execute that first:

$result1 = mysql_query("SET @running_sum = 0;");

$result2 = mysql_query($this->query); //

From the Manual:

mysql_query() sends a unique query (multiple queries are not supported)

Side note: The mysql_* library is deprecated, it is recommended to upgrade to a modern MySQL library such as PDO or MySQLi.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值