Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.