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

mdobrenko

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

?>
Joe T

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL query works in PHPMyAdmin but not PHP

From Dev

Mysql query works in Phpmyadmin but not works in PHP

From Dev

MySQL SELECT query works in PHPmyadmin, not in PHP

From Dev

MySQL INSERT query works in Phpmyadmin but not in PHP

From Dev

mysql query works in phpmyadmin but errors in php

From Dev

Query works in phpmyadmin but not in PHP

From Dev

How can I get a mysql script that works in PHPmyadmin, to work in a PHP mysql query?

From Dev

mysql and php %like% query. Works in phpmyadmin but different results php

From Dev

UNION query works in mysql but doesn't work the same in php

From Dev

Query works with mysql but doesn't work with php sql call

From Dev

Query doesn't work on PHP but works directly in Mysql

From Dev

execute sql query in php doesn't work ,but the query work in phpmyadmin

From Dev

execute sql query in php doesn't work ,but the query work in phpmyadmin

From Dev

PHP Query won't work with DELETE

From Dev

MySQL works in terminal on PHPmyadmin but does not work in php script?

From Dev

MySQL UPDATE works in phpMyAdmin but not in PHP

From Dev

Mysql & PHP won't echo the results of the query

From Dev

A query that uses variables works in PHPMyAdmin, but not in a PHP script

From Dev

SQL Query works in phpMyAdmin but not in php page

From Dev

A query that uses variables works in PHPMyAdmin, but not in a PHP script

From Dev

Query work in PhpMyAdmin but does not work in php codes

From Dev

PHP and MySQL french accent works in PHPMyAdmin but not in page

From Dev

Why won't my php connection to mysql server work?

From Dev

PHP MySQL/PDO UPDATE Query Won't Execute

From Dev

MYSQL query won't return data using php

From Dev

query works in mysql terminal but not in php

From Dev

query works in mysql fails in php

From Dev

DB query from PHP gives no result but same query on phpMyAdmin works?

From Dev

PHP PDO MySQL function doesn't edit data, query works

Related Related

  1. 1

    MySQL query works in PHPMyAdmin but not PHP

  2. 2

    Mysql query works in Phpmyadmin but not works in PHP

  3. 3

    MySQL SELECT query works in PHPmyadmin, not in PHP

  4. 4

    MySQL INSERT query works in Phpmyadmin but not in PHP

  5. 5

    mysql query works in phpmyadmin but errors in php

  6. 6

    Query works in phpmyadmin but not in PHP

  7. 7

    How can I get a mysql script that works in PHPmyadmin, to work in a PHP mysql query?

  8. 8

    mysql and php %like% query. Works in phpmyadmin but different results php

  9. 9

    UNION query works in mysql but doesn't work the same in php

  10. 10

    Query works with mysql but doesn't work with php sql call

  11. 11

    Query doesn't work on PHP but works directly in Mysql

  12. 12

    execute sql query in php doesn't work ,but the query work in phpmyadmin

  13. 13

    execute sql query in php doesn't work ,but the query work in phpmyadmin

  14. 14

    PHP Query won't work with DELETE

  15. 15

    MySQL works in terminal on PHPmyadmin but does not work in php script?

  16. 16

    MySQL UPDATE works in phpMyAdmin but not in PHP

  17. 17

    Mysql & PHP won't echo the results of the query

  18. 18

    A query that uses variables works in PHPMyAdmin, but not in a PHP script

  19. 19

    SQL Query works in phpMyAdmin but not in php page

  20. 20

    A query that uses variables works in PHPMyAdmin, but not in a PHP script

  21. 21

    Query work in PhpMyAdmin but does not work in php codes

  22. 22

    PHP and MySQL french accent works in PHPMyAdmin but not in page

  23. 23

    Why won't my php connection to mysql server work?

  24. 24

    PHP MySQL/PDO UPDATE Query Won't Execute

  25. 25

    MYSQL query won't return data using php

  26. 26

    query works in mysql terminal but not in php

  27. 27

    query works in mysql fails in php

  28. 28

    DB query from PHP gives no result but same query on phpMyAdmin works?

  29. 29

    PHP PDO MySQL function doesn't edit data, query works

HotTag

Archive