SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member wayzer's Avatar
    Join Date
    Aug 2004
    Location
    Kingston
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a table with mysql and php.

    Hello me again,

    I am trying to create a table in php connecting to a mysql DB. I have tried a number of different formats and they have not worked. I have searched the internet for more examples and nothing is working. Could someone maybe take a look at it and tell me what I am doing wrong. I will give you my most recent code attempt.

    <?php

    $connection=mysql_connect("localhost", "root", "") or die("Could not connect!");
    mysql_select_db("tutorial_db", $connection) or die("Could not select a valid DB");

    $sql = CREATE TABLE person ( id INT NOT NULL AUTO_INCREMENT ,
    name VARCHAR( 35 ) DEFAULT 'Noname' NOT NULL ,
    surname VARCHAR( 35 ) DEFAULT 'Nosurname' NOT NULL ,
    sex VARCHAR( 1 ) NOT NULL ,
    age SMALLINT UNSIGNED NOT NULL ,
    PRIMARY KEY ( id ) );

    mysql_query($sql);
    mysql_close();

    ?>

    Thanks,

    Wayzer

  2. #2
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $connection
    =mysql_connect("localhost""root""") or die("Could not connect!");
    mysql_select_db("tutorial_db"$connection) or die("Could not select a valid DB");

    $sql "CREATE TABLE person ( id INT NOT NULL AUTO_INCREMENT ,
    name VARCHAR( 35 ) DEFAULT 'Noname' NOT NULL ,
    surname VARCHAR( 35 ) DEFAULT 'Nosurname' NOT NULL ,
    sex VARCHAR( 1 ) NOT NULL ,
    age SMALLINT UNSIGNED NOT NULL ,
    PRIMARY KEY ( id ) )"
    ;

    mysql_query($sql);
    mysql_close();
    ?>
    Anjanesh

  3. #3
    SitePoint Addict Adam A Flynn's Avatar
    Join Date
    Jul 2004
    Location
    Canada
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    www.mysql.com and download MySQLcc or google phpMyAdmin, download, and install that. They're both reasonably good MySQL front-ends, that will make creating/editing tables and such simple.

    Otherwise, mysql_query($sql) or print(mysql_error()); will print the MySQL error.

  4. #4
    SitePoint Member wayzer's Avatar
    Join Date
    Aug 2004
    Location
    Kingston
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, It was the double quotes around the create table string. Silly mistake I guess, but that is why we all love programming.

    Thanks again guys.


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
  •