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

PHP

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();

        ?>