creating a mysql table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carllucas
    New Member
    • Oct 2006
    • 35

    creating a mysql table

    I am green to mysql and php. I've got issues trying to create a table in my database

    Code:
    mysql_connect($hostname, $username, $password);
    mysql_select_db($database) or die("Unable to select database");
    
    $query="CREATE TABLE news (id int(6) NOT NULL,name varchar(15) NOT NULL, age int(2) NOT NULL,email varchar(20) NOT NULL)";
    
    mysql_quert($query);
    
    mysql_close();
    when i go to phpMyAdmin this table isn't there.

    I don't even know how to start troubleshooting this because I'm just learning how the system works. Can anyone offer suggestions.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Is the mysql_quert($qu ery); just a typo or did you actually use the mysql_quert command.?

    And do NOT DOUBLE POST!! The other one is removed by me. Next time I'll remove all of them.

    Ronald :cool:

    Comment

    • carllucas
      New Member
      • Oct 2006
      • 35

      #3
      sorry 'bout the double post. I just noticed that there is a means to 'move' a post.

      and yes, that was just a typo. My code looks like this:

      Code:
      $query="CREATE TABLE news (id int(6) NOT NULL,name varchar(15) NOT NULL, age int(2) NOT NULL,email varchar(20) NOT NULL)";
      
      mysql_query($query);
      but this doesn't work.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Your mysql_query produces a result, so you must assign it to a variable and then process that result. Do error processing on each of the MySQL statements. That way it is easier to see what a problem might be (not in this case because you did not have a result). Anyway, the following shows the correct code, but you still have to handle the result of your query. Probably this mshows the error encountered.
        [php]
        <?php
        mysql_connect($ hostname, $username, $password)
        or die ("Cannot connect: " . mysql_error());
        mysql_select_db ($database)
        or die("Unable to select database: " . mysql_error());

        $query="CREATE TABLE news (id int(6) NOT NULL,name varchar(15) NOT NULL, age int(2) NOT NULL,email varchar(20) NOT NULL)";

        $res = mysql_query($qu ery);
        if (!res)
        die("Invalid query: " . mysql_error());
        else
        echo 'Table created';
        mysql_close();
        ?>[/php]

        Ronald :cool:

        Comment

        • carllucas
          New Member
          • Oct 2006
          • 35

          #5
          excellent! worked!

          i added a '$' before 'res' on your code.

          so must u always assign a mysql_query to a variable?

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Sorry for the typo. The mysql_query command produces output, so you assign it to a variable. I expect that you'd always want to inspect the result of a mysql_ statement, so you would always assign it. I must confess that I have never run mysql_ commands without assigning it to a var, so I wouldn't know from experience what problems that would create.

            Ronald :cool:

            Comment

            Working...