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

标签 php mysql

<分区>

我有一个在 PHPMyAdmin 中成功运行的 MYSQl 查询。然而,当试图将它作为字符串存储在 PHP 变量中,然后使用 my_sqli 查询时,它给了我一个“boolean 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);
    }
}

?>

最佳答案

看起来您正在尝试使用单个 mysqli_query() 执行多个查询;

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

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

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

只需这样做:

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

关于PHP MYSQL 查询在 PHP 中不起作用,但在 PHPMyAdmin 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24963653/

相关文章:

php - mysql 两个查询

sql - ms-access:从另一个查询中选择

mysql - 错误: No module named 'mysql'

PHP 脚本完全删除字符串 - 如何修复?

php - 从 PHP 重启 CentOS 服务器? (不是php服务而是服务器)

php - 在 Laravel 中使用同步分离数据透视表

MySQL 搜索查询按匹配相关性排序

php - 多个 PHP mysqli 准备好的语句(INSERT UPDATE SELECT)奇怪的行为

php - 这个符号.*是什么?在 PHP 中表示(HTML 解析)

php - 如何处理一个文章网站的多个分类条目