1

I have a MYSQl query which successfully runs in PHPMyAdmin. However, when trying to store it as a string in a PHP variable, and then using the my_sqli query, it gives me a "boolean false". What can I do to get this working? I have a feeling it might have to do with some of the sequences of characters in the query itself that are conflicting with the string standards in PHP.

The PHP code:

<?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);
    }
}

?>
boisterouslobster
  • 1,283
  • 3
  • 25
  • 54

1 Answers1

2

Looks like you're trying to execute multiple queries with a single mysqli_query();

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

Multiple statements or multi queries must be executed with mysqli_multi_query().

And a similar question here: Two mysqli queries

Just do this instead:

$result = mysqli_multi_query($con, $query);
Community
  • 1
  • 1
Joe T
  • 2,300
  • 1
  • 19
  • 31