SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create mysql tables in array

    Greetings!

    I'm not sure if it's allowed to run 2 mysql queries to create 2 tables with php, I've tried to do that with this array :

    PHP Code:
    <?
    $table_fields 
    = array (
                    
    table_name => array(=> "table_users"=> "table_links"),
                    
    field_1 => array(=> "userID"=> "linkID"),
                    
    field_2 => array(=> "username"=> "url"),
                    
    field_3 => array(=> "real_name"=> "clicks"),
                    
    field_4 => array(=> "password"=> "title"),
                    
    field_5 => array(=> "level"=> "date"),
                    
    vartype_1 => array(=> "int"=> "int"),
                    
    vartype_2 => array(=> "varchar(15)"=> "varchar(350)"),
                    
    vartype_3 => array(=> "varchar(35)"=> "int(11)"),
                    
    vartype_4 => array(=> "varchar(35)"=> "varchar(50)"),
                    
    vartype_5 => array(=> "int(2)"=> "CURRENT_DATE")
                    );
                    
                    for (
    $i 1$i <= 2$i++)
                        {
                        
    $sql "CREATE TABLE".$table_fields[table_name][$i]."
                                ("
    .
                                
    $table_fields[field_1][$i]." ".$table_fields[vartype_1][$i]." NOT NULL AUTO_INCREMENT,
                                PRIMARY KEY("
    .$table_fields[field_1][$i].",".
                                
    $table_fields[field_2][$i]." ".$table_fields[vartype_2][$i].",".
                                
    $table_fields[field_3][$i]." ".$table_fields[vartype_3][$i].",".
                                
    $table_fields[field_4][$i]." ".$table_fields[vartype_4][$i].",".
                                
    $table_fields[field_5][$i]." ".$table_fields[vartype_5][$i]."
                                )"
    ;
                        
                        if (
    mysql_query($sql,$con))
                            {
                            
    table_ok('Table '.$table_fields[table_name][$i].' created successfully!');
                            }
                        else
                            {
                            
    error('Error creating table '.$table_fields[table_name][$i].'!');
                            }
                        
                        }
    ?>
    the script is however calling the function error, and reports :
    Code:
    Error creating table table_users!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 'varchar(15), real_name varchar(35), password varchar(35), level int(2) )' at line 3
    I've checked the syntax 100 times and it seems OK to me.... Is it something else I missed?

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's because you're missing the closing parentheses for the PRIMARY KEY( part

    You can call mysql_query as many times as you want in a script

    But I have no idea why you would want to create this convoluted thing instead of just writing the two CREATE TABLE queries

    35 lines of code for the equivalent of

    PHP Code:
    mysql_query("CREATE TABLE table_users (user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username varchar(15), real_name varchar(35), password varchar(35), level int(2))");

    mysql_query("CREATE TABLE table_links (linkID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, url varchar(350), clicks int(11), title varchar(50), date DATE)"); 

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    It's because you're missing the closing parentheses for the PRIMARY KEY( part

    You can call mysql_query as many times as you want in a script

    But I have no idea why you would want to create this convoluted thing instead of just writing the two CREATE TABLE queries

    35 lines of code for the equivalent of

    PHP Code:
    mysql_query("CREATE TABLE table_users (user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username varchar(15), real_name varchar(35), password varchar(35), level int(2))");

    mysql_query("CREATE TABLE table_links (linkID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, url varchar(350), clicks int(11), title varchar(50), date DATE)"); 
    I should have checked 101st time!
    Actually I will have a huge number of tables with the same structure that's why I decided to call the query in a loop!
    Thanks a lot!

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Fair enough. No problem, a fresh set of eyes usually do the trick for catching typos


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
  •