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!

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'

$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","");

    $sql ='CREATE TABLE IF NOT EXISTS testing (
        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("SET cubScout 'utf8mb4'");
    echo "Created Table.\n";
} catch (Exception $e) {
    echo $e->getMessage();

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

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

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