PHP MYSQL查询在PHP中不起作用,但在PHPMyAdmin中起作用

时间:2014-07-25 20:04:39

标签: php mysql

我有一个MYSQl查询,它可以在PHPMyAdmin中成功运行。但是,当尝试将其作为字符串存储在PHP变量中,然后使用my_sqli查询时,它会给我一个"布尔值false"。我该怎么做才能让这个工作?我有一种感觉,它可能与查询本身中与PHP中的字符串标准冲突的一些字符序列有关。

PHP代码:

<?php
$query = 
"SET @pro_today = (SELECT count(*)
FROM `subscriptions`
WHERE CAST(created_at as DATE) = CAST(now() as DATE)
AND cancelled = 0
AND subscription_id != ''
AND plan = 'pro');

SET @pro_this_week = (SELECT count(*) 
FROM `subscriptions` 
WHERE WEEKOFYEAR(created_at)= WEEKOFYEAR(NOW())
AND cancelled = 0
AND subscription_id != ''
AND plan = 'pro');

SET @pro_this_month = (
SELECT count(*)
FROM `subscriptions`
WHERE MONTH(created_at)= MONTH(NOW())
AND cancelled =0
AND subscription_id !=''
AND plan = 'pro');

SET @single_event_today = (SELECT count(*)
FROM `subscriptions`
WHERE CAST(created_at as DATE) = CAST(now() as DATE)
AND cancelled = 0
AND subscription_id != ''
AND plan = 'single-event');

SET @single_event_this_week = 
(SELECT count(*) 
FROM `subscriptions` 
WHERE WEEKOFYEAR(created_at)= WEEKOFYEAR(NOW())
AND cancelled = 0
AND subscription_id != ''
AND plan = 'single-event');

SET @single_event_this_month = (SELECT count(*) 
FROM `subscriptions` 
WHERE MONTH(created_at)= MONTH(NOW())
AND cancelled = 0
AND subscription_id != ''
AND plan = 'single-event');

UPDATE statistics
SET statistics.single_event_today = @single_event_today,
statistics.single_event_this_week = @single_event_this_week,
statistics.single_event_this_month = @single_event_this_month,

statistics.premier_today = @pro_today,
statistics.premier_this_week = @pro_this_week,
statistics.premier_this_month = @pro_this_month,

statistics.revenue_today = ((@pro_today * 8 ) + (@single_event_today * 25)),
statistics.revenue_this_week = ((@pro_this_week * 8)+(@single_event_this_week * 25)),
statistics.revenue_this_month = ((@pro_this_month * 8)+(@single_event_this_month * 25));";

doQuery($query);

function doQuery($query)
{
    $con = mysqli_connect(//correct connection settings);

    if(mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL : " . mysqli_connect_error();
    }

    $result = mysqli_query($con , $query);
    mysqli_close($con);

    if($result)
    {
        echo "Success";
    }
    else{
    var_dump($result);
    }
}

?>

1 个答案:

答案 0 :(得分:2)

您似乎尝试使用单个mysqli_query()执行多个查询;

http://php.net/manual/en/mysqli.quickstart.multiple-statement.php

  

必须执行多个语句或多个查询   mysqli_multi_query()。

这里有一个类似的问题: Two mysqli queries

请改为:

$result = mysqli_multi_query($con, $query);