'PHP tag is closing with code in query $db->

Forgive me, as I've not worked in php for years. I'm picking up some old code to get working again and I'm having a strange issue.

I'm writing in php with wordpress. As I am editing the code, I've noticed the php tag is closing after the following:

<?php
$databaseHost = "Localhost";
$databaseName = "testDB";
$databaseUser = "TESTUSER";
$databasePassword = "TESTPASS";

$coin_id = (isset($_POST['coin_id'])) ? $_POST['coin_id'] : '';

try {
    $db = new PDO('mysql:host=' . $databaseHost . ';dbname=' . $databaseName . ';charset=utf8', $databaseUser, $databasePassword);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $request = "SELECT
    _7UR_participants_database.city,
            _7UR_participants_database.state,
            _7UR_participants_database.country,
            _7UR_participants_database.zip,
            _7UR_participants_database.coin_id,
            FROM _7UR_participants_database GROUP BY _7UR_participants_database.coin_id ASC";           
$stmt = $db->query($request);
$item_info = $stmt->fetchAll(); 
} catch (PDOException $e) {
    echo "Exception: " . $e->getMessage(); 
    exit;
} // Try / catch end
?>

Everything after that > following $db- is not included in the php. The php tag is closing with that last >. Do I need to escape the character or something of that nature?



Solution 1:[1]

since your formatting is hard to undertand i rewrite it on my liking that it "migth" actually works

 $dsn = "mysql:host=localhost;dbname=testDB;charset=utf8mb4";  // most cool kids use charset=utf8mb4 
        $options = [
          PDO::ATTR_EMULATE_PREPARES   => false, // turn off emulation mode for "real" prepared statements
          PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
        ];
        try {
          $dbh = new PDO($dsn, "TESTUSER", "TESTPASS", $options);
        } catch (Exception $e) {
          error_log($e->getMessage());
          echo ("Error Code: " . $e->getCode() . "<br>"); // never use echo on public release build it would leak your database credential this is optional great for troubleshooting 
          echo ("Error Message: " . $e->getMessage() . "<br>");
          exit('Something weird happened');//
        }
       $request = $dbh->prepare("SELECT
        _7UR_participants_database.city,
                _7UR_participants_database.state,
                _7UR_participants_database.country,
                _7UR_participants_database.zip,
                _7UR_participants_database.coin_id,
                FROM _7UR_participants_database GROUP BY _7UR_participants_database.coin_id ASC";
                
    $request->execute([]);  // you do not have something like this also i never put value since i don't know what your doing
    $item_info = $request->fetchAll(); // store the fetched on $item_info also you need to indicate what data type you fetching by default it always PDO::FETCH_ASSOC

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Presner Pontillas