Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询在db客户机上运行良好,但PHP给出SQLSTATE[42000]错误_Php_Mysql_Database - Fatal编程技术网

MySQL查询在db客户机上运行良好,但PHP给出SQLSTATE[42000]错误

MySQL查询在db客户机上运行良好,但PHP给出SQLSTATE[42000]错误,php,mysql,database,Php,Mysql,Database,我有一种奇怪的情况。长话短说,我有一个MySQL查询,当PHP运行它时,它会给出 警告:PDOStatement::execute():SQLSTATE[42000]:语法错误或 访问冲突:1064您的SQL语法有错误;检查 与右边的MySQL服务器版本相对应的手册 在第6行的“”附近使用的语法 但是,当我在数据库客户端应用程序(如DataGrip或SequelPro)上运行它时,它工作正常并返回行集 echo $sQuery; // I print the query right before

我有一种奇怪的情况。长话短说,我有一个MySQL查询,当PHP运行它时,它会给出

警告:PDOStatement::execute():SQLSTATE[42000]:语法错误或 访问冲突:1064您的SQL语法有错误;检查 与右边的MySQL服务器版本相对应的手册 在第6行的“”附近使用的语法

但是,当我在数据库客户端应用程序(如DataGrip或SequelPro)上运行它时,它工作正常并返回行集

echo $sQuery; // I print the query right before the execution
$stmt = $dbh->prepare ($sQuery);
$stmt->execute(); // This is the line that gives the error
$rResult = $stmt->fetchAll();
unset ($stmt); 
那么,你们中有人以前遇到过这种奇怪的问题吗?解决这个问题的最佳方法是什么

查询:

SELECT SQL_CALC_FOUND_ROWS max(r.id) AS id
    ,r.client_name AS client_name
    ,r.ban_no AS ban_no
    ,r.ban_type
    ,(
        CASE 
            WHEN c.total_subscribers IS NOT NULL
                THEN c.total_subscribers
            ELSE r.subscribers
            END
        ) AS subscribers
    ,r.deal_value
    ,(
        CASE 
            WHEN c.potential_renewals IS NOT NULL
                THEN c.potential_renewals
            ELSE r.potential_renewals
            END
        ) AS potential_renewals
    ,r.prev_sales_rep_id
    ,r.sales_rep_id
    ,r.city
    ,ce.postal_code AS postal_code
    ,r.outlet_id
    ,(
        CASE 
            WHEN (
                    IFNULL(r.activity_type, 0) != '0'
                    AND IFNULL(r.activity_type, 0) != '4'
                    AND IFNULL(r.activity_type, 0) != '5'
                    AND (r.last_spoken IS NOT NULL)
                    AND (to_days(r.last_spoken) + 15 > to_days(r.call_date))
                    )
                THEN 'Yes'
            ELSE 'No'
            END
        ) AS activity_type
    ,r.ban_type AS STATUS
FROM TableR r
LEFT JOIN (
    SELECT ban_id
        ,type
        ,date_added
        ,COALESCE(total_subscribers, 0) AS total_subscribers
        ,COALESCE(potential_renewals, 0) AS potential_renewals
    FROM TableCS cs1
    WHERE cs1.ban_id IN (
            '6375305622668619'
            ,'7852790096027066'
            )
    GROUP BY cs1.ban_id
    ) c ON r.ban_no = c.ban_id
LEFT JOIN TableCE ce ON r.ban_no = ce.ban_no
WHERE (r.ban_type != 'D')
    AND r.dealer_id = '15'
GROUP BY r.ban_no
ORDER BY `subscribers` DESC LIMIT 0
    ,50;
好吧,那么

我想不出原因但是

$list_of_bansas_string .= '\''.$call_list_record[0].'\','; // we used single quote to solve the problem
这是第一个原因。因此,使用单引号而不是双引号是安全的

$list_of_bansas_string[strlen($list_of_bansas_string)-1] = '  '; // we can't assign empty string, we should put space on it 

实际上,PHP不允许我分配空字符。因此,我最终用空格字符替换了它,它起了作用。

您的查询是什么?对我来说,这听起来像是一个引用问题和/或空值,但在看到完整的代码之前,我们不会知道这一点。因为您要么留下了问题,要么试图自己解决它,没有人能回答这个问题。我自己留下了这个问题。祝你好运。@Fred ii-没关系。非常感谢。问题是,如果查询出现问题,它在db客户端应用程序上也不会正常工作。这就是为什么我没有把我的问题包括在这里。我只是想知道以前是否有人遇到过类似的问题。基本上,您使用的是预先准备好的语句,没有绑定任何参数,也没有手动提供用户输入。