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.

PHP

Samuel Mamulaschwili
Samuel Mamulaschwili
9,935 Points

Mysql CREATE TABLE Problem!

hey,

I`m trying to create a mysql table, but stuck with a problem.

The error message:

Connected successfullyCould not create table 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 '(14) NOT NULL, primary key ( emp_id ))' at line 1

My code is:

<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'root'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Verbindung zur Datenbank nicht möglich. ' . mysql_error()); } echo 'Connected successfully'; $sql = 'CREATE TABLE employee( '. 'emp_id INT NOT NULL AUTO_INCREMENT, '. 'emp_name VARCHAR(20) NOT NULL, '. 'emp_address VARCHAR(20) NOT NULL, '. 'emp_salary INT NOT NULL, '. 'join_date timestamp(14) NOT NULL, '. 'primary key ( emp_id ))';

mysql_select_db('test_db'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not create table ' . mysql_error()); } echo "Table create successfully\n"; mysql_close($conn); ?>

What`s wrong???

Thanks for help Sam

1 Answer

Hi Samuel,

I'm no expert at php or SQL but I modified your code and this worked for me. I'm running mySql version 5.5. Hope it works for you. Here is a link you might be interested in: MySQL TIMESTAMP. Viel Glück!

Jeff

<?php

        $dbhost = 'localhost';
        $dbuser = 'xxxxx';
        $dbpass = 'xxxxx';
        $conn = new  mysqli($dbhost, $dbuser, $dbpass);
        if(!$conn ) {
            die('Verbindung zur Datenbank nicht möglich. ' . mysqli_error($conn));
        }

        echo 'Connected successfully';

        $conn->select_db('test_db');
        echo ('Selected the test_db database <br>');

        $sql = 'CREATE TABLE employee(emp_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
            emp_name VARCHAR(20) NOT NULL,
            emp_address VARCHAR(20) NOT NULL,
            emp_salary INT NOT NULL,
            join_date timestamp NOT NULL)';


         // $retval = mysql_query( $sql, $conn );
         if(!$conn->query($sql)) {
            die('Could not create table ' . mysqli_error($conn));
         }

         echo "Table create successfully\n";
         $conn->close();

        ?>