PHP查询中的MYSQL变量 [英] MYSQL variables in php query

查看:154
本文介绍了PHP查询中的MYSQL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决这个问题的方法可能只是我的一个简单的发现.

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

我正在尝试运行以字符串形式存储在PHP中的MYSQL查询.使用Navicat之类的DBM工具,查询运行良好,但在我的PHP开发环境中返回false.有我看过的东西吗?

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是一个字符串,用于保存上面的查询,如上面向您显示的那样.

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

推荐答案

问题是mysql_query()不支持多个查询.您的SET @running_sum = 0;被认为是单独的查询,因此您必须先执行该查询:

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); // <-- without the SET ... query

手册:

mysql_query()发送一个唯一查询(不支持多个查询)

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


侧面说明:不建议使用mysql_*库,建议升级到现代MySQL库,例如 PDO MySQLi .


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

这篇关于PHP查询中的MYSQL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆