ianhaney10 Posted June 2 Share Posted June 2 (edited) I got a php script for newsletters mailing system and uploaded it to my website and after I log in to the admin side, I get the following error Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups g JOIN group_subscribers gs ON gs.group_id = g.id AND gs.subscriber_id = ' at line 4 in /home/itdoneri/public_html/newsletters/admin/index.php:16 Stack trace: #0 /home/itdoneri/public_html/newsletters/admin/index.php(16): PDOStatement->execute(Array) #1 {main} thrown in /home/itdoneri/public_html/newsletters/admin/index.php on line 16 The section of code is below // Get the total number of new subscribers within the last day $stmt = $pdo->prepare('SELECT s.*, ((SELECT COUNT(*) FROM campaign_items ci WHERE ci.subscriber_id = s.id AND ci.status = "Completed") / ((SELECT COUNT(*) FROM campaign_items ci WHERE ci.subscriber_id = s.id AND ci.status = "Completed") + (SELECT COUNT(*) FROM campaign_items ci WHERE ci.subscriber_id = s.id AND ci.status = "Failed")) * 100) AS percent_received, (SELECT GROUP_CONCAT(g.title, ",") FROM groups g JOIN group_subscribers gs ON gs.group_id = g.id AND gs.subscriber_id = s.id) AS groups FROM subscribers s WHERE cast(s.date_subbed as DATE) = cast(? as DATE) ORDER BY s.date_subbed DESC'); $stmt->execute([$date]); $subscribers = $stmt->fetchAll(PDO::FETCH_ASSOC); I was seeing if anyone could help please. I did try putting the table name in back comma like `groups` but that didn't work I may have found the issue, I found out groups is a reserved word so updated the code to the following and looks like it's worked $stmt = $pdo->prepare('SELECT s.*, ((SELECT COUNT(*) FROM campaign_items ci WHERE ci.subscriber_id = s.id AND ci.status = "Completed") / ((SELECT COUNT(*) FROM campaign_items ci WHERE ci.subscriber_id = s.id AND ci.status = "Completed") + (SELECT COUNT(*) FROM campaign_items ci WHERE ci.subscriber_id = s.id AND ci.status = "Failed")) * 100) AS percent_received, (SELECT GROUP_CONCAT(g.title, ",") FROM `groups` g JOIN group_subscribers gs ON gs.group_id = g.id AND gs.subscriber_id = s.id) AS group_titles FROM subscribers s WHERE cast(s.date_subbed as DATE) = cast(? as DATE) ORDER BY s.date_subbed DESC'); $stmt->execute([$date]); Edited June 2 by ianhaney10 Quote Link to comment https://forums.phpfreaks.com/topic/328224-fatal-error-uncaught-pdoexception-sqlstate42000-error/ Share on other sites More sharing options...
gizmola Posted June 2 Share Posted June 2 How do you "get a script" that doesn't work without alteration of the the SQL code? Was it designed to use a different database? Yes GROUPS is a MySQL reserved/keyword You took care of this correctly by using the backtick character to quote the groups table name Quote Link to comment https://forums.phpfreaks.com/topic/328224-fatal-error-uncaught-pdoexception-sqlstate42000-error/#findComment-1654492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.