Monday 18 December 2017

PHP MYSQL query won't work in PHP but works in PHPMyAdmin





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:



$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);

}
}

?>


Answer




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



href="http://php.net/manual/en/mysqli.quickstart.multiple-statement.php" rel="nofollow
noreferrer">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:
href="https://stackoverflow.com/questions/10924127/two-mysqli-queries">Two mysqli
queries



Just do this instead:




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


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...