SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Old Fogey mancroft's Avatar
    Join Date
    Oct 2002
    Location
    lost
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create table error

    Hello

    I am trying to create a table using the code below and am getting the error:

    Datbase ERROR: CREATE TABLE links ( id INT NOT NULL AUTO_INCREMENT, sitename VARCHAR(50), siteurl VARCHAR(75), description TEXT, category VARCHAR(50), PRIMARY KEY(id) );
    You have an error in your SQL syntax near '; ' at line 1Create Table Results RESULT =


    Any ideas as to what the problem is?

    <?
    $usr = "mancroft";
    $pwd = "xxxxxxxx";
    $db = "mancroft_linksdb";
    $host = "localhost";
    /*== CONNECT TO DATABASE ==*/
    $cid = mysql_connect($host,$usr,$pwd);
    if (!$cid) { print "ERROR: " . mysql_error() . "\n"; }

    #select database to use
    mysql_select_db($db, $cid);

    /*== SETUP SQL STATEMENT ==*/
    # create links table
    $sql = " CREATE TABLE links (";
    $sql .= " id INT NOT NULL AUTO_INCREMENT, ";
    $sql .= " sitename VARCHAR(50), ";
    $sql .= " siteurl VARCHAR(75), ";
    $sql .= " description TEXT, ";
    $sql .= " category VARCHAR(50), ";
    $sql .= " PRIMARY KEY(id) ";
    $sql .= " ); ";

    /*== EXECUTE SQL STATEMENT ==*/
    $result = mysql_query($sql, $cid);
    if (mysql_error()) { print "Datbase ERROR: $sql <br>" . mysql_error(); }
    # display result for table creation query
    # this should be 1 on a success
    print "Create Table Results RESULT = $result\n\n";
    ?>
    :

    :

  2. #2
    SitePoint Enthusiast HogMan's Avatar
    Join Date
    Mar 2003
    Location
    Nebraska
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Odd, I cut and pasted your code and only changed the username/password/db and it worked just fine on my system. Also, one note on the sql statement which might make it easier to read.

    PHP Code:
    <?php

    $sql 
    "
        CREATE TABLE garlinks (
        id INT NOT NULL AUTO_INCREMENT,
        sitename VARCHAR(50),
        siteurl VARCHAR(75),
        description TEXT,
        category VARCHAR(50),
        PRIMARY KEY(id)
        );
    "
    ;
    If you take your sql statment and try running it manually, does that work at all? For example, just using the mysql command line or phpMyAdmin.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't think this would cause such an error (rather, I would think it would be ignored), but you don't need the semi-colon at the end of your SQL statement when running it from PHP. Other than that I'm not seeing anything strange.

  4. #4
    SitePoint Zealot basicwebus's Avatar
    Join Date
    Jul 2003
    Location
    Missouri
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need a semicolon after the last entry like this:PRIMARY KEY(id);
    After the third time of reading the instructions,
    it finally soaks in to my thick skull.




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •