MySQL提取结果只是查询在Mysql工作台中正常工作

时间:2014-05-13 06:38:26

标签: php mysql

MySQL提取结果只是查询在Mysql工作台中正常工作:

$sql1="set @num := 0, @type := '';
select sum(t1.amount)as total_bal from (select cat1.customer_account_num, cat1.amount, @num := if(@type = cat1.customer_account_num, @num + 1, 1) as row_number, @type := cat1.customer_account_num as dummy,cat1.date_of_transaction from customer_account_transaction AS cat1, customer_account AS a, plan AS p where cat1.customer_account_num=a.customer_account_num and a.planid=p.planid and p.plantype=1 and a.executive='AV/E105' order by cat1.customer_account_num, cat1.date_of_transaction asc) as t1 where t1.row_number>1 and t1.date_of_transaction BETWEEN '2014-04-06' AND '2014-05-10';";

$result1=mysqli_query($GLOBALS['con'],$sql1);
    $daily_bal='';  
    if($result1)
    {   
        $row1=mysqli_fetch_array($result1);
        $daily_bal=$row1['total_bal'];
        echo $daily_bal;
    }

2 个答案:

答案 0 :(得分:2)

您无法以this的方式执行多个查询。你需要使用:

mysqli_multi_query()

来自php.net的这个片段可以帮助你。

if (!$mysqli->multi_query($sql)) {
    echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
    if ($res = $mysqli->store_result()) {
        var_dump($res->fetch_all(MYSQLI_ASSOC));
        $res->free();
    }
} while ($mysqli->more_results() && $mysqli->next_result());

答案 1 :(得分:1)

谢谢你ʀɛɔʘɴʀɛɔʘɴ 我改变了我的查询,如下所示,它有效。

$sql1="set @num := 0, @type := '';
select sum(t1.amount)as total_bal from (select cat1.customer_account_num, cat1.amount, @num := if(@type = cat1.customer_account_num, @num + 1, 1) as row_number, @type := cat1.customer_account_num as dummy,cat1.date_of_transaction from customer_account_transaction AS cat1, customer_account AS a, plan AS p where cat1.customer_account_num=a.customer_account_num and a.planid=p.planid and p.plantype=1 and a.executive='AV/E105' order by cat1.customer_account_num, cat1.date_of_transaction asc) as t1 where t1.row_number>1 and t1.date_of_transaction BETWEEN '2014-04-06' AND '2014-05-10';";

if (!mysqli_multi_query($GLOBALS['con'],$sql1)) {
    echo "Multi query failed: (" . mysqli_errno() . ") " . mysqli_error();
}
    $daily_bal='';  
    do {
        if ($result1 = mysqli_store_result($GLOBALS['con'])) {
            //var_dump(mysqli_fetch_all($result1,MYSQLI_ASSOC));
            $row1=mysqli_fetch_array($result1);
            $daily_bal=$row1['total_bal'];

            mysqli_free_result($result1);
        }
    } while (mysqli_more_results($GLOBALS['con']) && mysqli_next_result($GLOBALS['con']));