-1
<?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";
?> 

I have recently been learning php and I am trying to get data a query from MySQL but I keep getting a syntax error. I know I have one but I just don't know where and how many due to all the keywords I'm not sure of. I am also trying to figure out how to output the data or will the catch and try methods output it themselves, that is why I have an echo statement calling the query at the end.

CJones
  • 17
  • 2
  • 2
    Do not use backticks around non column names, EG `COUNT(*)` or `FROM`... – Usagi Miyamoto Jul 11 '17 at 14:17
  • 1
    Also, the value of `primary_phone `, "(000) 000-0000", in the `WHERE` clause should use quotes not backticks. – Matt Rink Jul 11 '17 at 14:19
  • Have you [tried the first hit when google'ing "mysql error 1604"](https://stackoverflow.com/questions/24658491/error-1604-wrong-syntax-mysql) ? – rkeet Jul 11 '17 at 14:20

2 Answers2

1

remove backtics from FROM and count(*) and use quote for literal '(000) 000-0000' and 'ACTIVE'

    $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";
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Backticks are used to quote column names and other user defined identifiers. Single quotes are used to enclose string constants.

Try something like this:

$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";
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33