Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Creating Tables and Manipulating Data with SQL Creating a Table

Table created but returns an error.

I am using the following code to create a table. The table is created, but I get the following error: SQLSTATE[HY000]: General error: 1193 Unknown system variable 'cubScout'

<?php
require_once('pass.php');
$db_connection = '"mysql:host=localhost;dbname=' . DB_NAME . '","' . DB_USERNAME . '","' . DB_PASS . '"';

// call database
try {
    $db = new PDO("mysql:host=localhost;dbname=cubScout;charset=utf8mb4","root","");
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    $sql ='CREATE TABLE IF NOT EXISTS testing (
        id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        FirstName VARCHAR( 50 ) NOT NULL, 
        LastName VARCHAR( 50 ) NOT NULL,
        Den VARCHAR( 2 ) NOT NULL, 
        Bobcat VARCHAR( 150 ), 
        Tiger VARCHAR( 150 ), 
        Wolf VARCHAR( 100 ),
        Bear VARCHAR( 100 ),
        Webelo VARCHAR( 100 ),
        Arrow VARCHAR( 100 ),
        Health VARCHAR( 500 ),
        CampMoney DECIMAL(5,2))';
    $db->exec($sql);
    $db->exec("SET cubScout 'utf8mb4'");
    echo "Created Table.\n";
} catch (Exception $e) {
    echo $e->getMessage();
    exit;
}

The code above the try is not relevant to the error. It is not used in the try and does not return an error on its own.

Thank you in advance.

1 Answer

The error was from the

<?php
$db->exec("SET cubScouts 'utf8mb4'");

When I change cubScouts to NAMES, the error goes away.