Jump to content

Recommended Posts

Posted (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 by ianhaney10

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? 

  1. Yes GROUPS is a MySQL reserved/keyword
  2. You took care of this correctly by using the backtick character to quote the groups table name

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.