PHP错误1604:访问MySQL查询并输出它

时间:2017-07-11 14:14:43

标签: php mysql

<?php
////////////////////////////////////////////////
//
error_reporting(E_ALL ^ E_NOTICE);
require_once ('../include/ISS_consts.inc');
require_once ('../include/connect_to_db.inc');
require_once ('../include/utils.inc');
$query = "SELECT `first_name`, `last_name`, `mi`, `primary_phone` , `id`,`COUNT(*)` `FROM` `member_tbl` WHERE status = `ACTIVE` AND `primary_phone` <> `(000) 000-0000` GROUP BY first_name, last_name, primary_phone HAVING  COUNT(*) > 1";
try
{
$stmt = $pdo->prepare($query);
$stmt->execute();

$stmt = null;
}
catch(PDOException $e)
{
echo "ERROR: ".$e->getMessage()."\n".$query."\n";
}

echo "This is the data".$query."\n";
?> 

我最近一直在学习php,我试图从MySQL获取数据查询,但我一直收到语法错误。我知道我有一个,但我不知道由于我不确定的所有关键字的位置和数量。我也试图找出如何输出数据或者catch和try方法自己输出它,这就是为什么我有一个echo语句在最后调用查询。

2 个答案:

答案 0 :(得分:1)

从FROM中删除背景并计算(*)并使用文字&000;(000)000-0000&#39;和&#39; ACTIVE&#39;

    $query = "SELECT `first_name`, `last_name`, `mi`, `primary_phone` , `id`,COUNT(*) 
    FROM `member_tbl` 
    WHERE status = 'ACTIVE' AND `primary_phone` <> '(000) 000-0000' 
    GROUP BY first_name, last_name, primary_phone 
    HAVING  COUNT(*) > 1";

答案 1 :(得分:0)

反引号用于引用列名和其他用户定义的标识符。单引号用于包含字符串常量。

尝试这样的事情:

$query = "
SELECT `first_name`, `last_name`, `mi`, `primary_phone` , `id`, COUNT(*)
  FROM `member_tbl`
  WHERE status = 'ACTIVE' AND `primary_phone` <> '(000) 000-0000'
  GROUP BY first_name, last_name, primary_phone
  HAVING  COUNT(*) > 1";