Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7841939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:12:24+00:00 2026-06-02T16:12:24+00:00

I would like your help with something that I have searched a lot but

  • 0

I would like your help with something that I have searched a lot but could’t yet find an answer.
As the title of the Question states, I am trying to create a table in mySql database in which I do not know the data types of the columns. Basically, I use php to create new tables in my database from an csv file. The first line contains the column names. I know that it can be done because I have seen it in phpMyAdmin where I can select a csv file and use the first line to generate the table column names and I do not have to specify the data types of each column,

So far, this is my php code :

$databaseName="myDatabase";
mysql_connect("localhost","xxxxxx","xxxxxxxx");
mysql_select_db($databaseName);


for($i = 0; $i < count($newFiles); $i++){//traverse the table that contains the file names
         $content = file($newFileLocation.$newFiles[$i]);

        //First line: $content[0];
        $firstLine=$content[0];//save first line which are the column names
        //echo  $firstLine;
        $tableName= str_replace('.txt','', $newFiles[$i]);//remove the .txt to use for table name
        //echo $tableName."\n";

        echo "\n\ncreating tables\n\n";
        /*mysql_query("load data local infile '$newFiles[$i]' 
               into table $tableName
                   fields terminated by ','
                   lines terminated by '\n'
                   ($firstLine) "); */


        mysql_close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:12:25+00:00Added an answer on June 2, 2026 at 4:12 pm

    Here is the solution to the problem. At first, we take the first line of the file to save the cols names(so that we know how to create the table). We save it in a string and then we separate them based on the comma character.
    After that, we take the second line of the file which contains values. We store that line to a string and we separate the string to it’s different values based on the comma character. We will be using this to identify the types of each column.

    So, this is the code.

    $databaseName="myDatabase";
    mysql_connect("localhost","xxxxxx","xxxxxxxx");
    mysql_select_db($databaseName);
    
    
    for($k = 0; $k < count($newFiles); $k++){//traverse the table that contains the file names
                $content = file($newFileLocation.$newFiles[$k]);
    
                //First line: $content[0];
                 $firstLine=$content[0];
                 $secondLine=$content[1];   
                //echo $firstLine."\n";
                $firstLine = str_replace("\r\n",'',$firstLine);
                $colNames=explode(',',$firstLine);
                $fieldList = explode(',',$secondLine);
    
                $dataType="";
                for($i = 0; $i < count($fieldList); $i++){
                    if (is_numeric($fieldList[$i])){
                            if (strpos($fieldList[$i],'.') == false){
                                $fieldList[$i] = (int)$fieldList[$i];
                           }else{
                                $fieldList[$i] = (float)$fieldList[$i];
                            }
                        }
    
                    switch(gettype($fieldList[$i])) {
                            case 'integer':
                                $typeInfo = 'int(11)';
                                break;
                            case 'float':
                            case 'double':
                                    $typeInfo = 'float';
                                    break;
    
                            case 'string':
                                    $typeInfo = 'varchar(80)';
                                break;
                            default:
                                    $typeInfo = 'varchar(80)';
                                    break;
                            }
    
                    if(gettype($fieldList[$i]) != NULL) {
                        $dataType= $dataType.'`'.$colNames[$i].'` '." ".$typeInfo.' NOT NULL';
                        if(($i)!=count($fieldList)-1){
                            $dataType= $dataType.",";
                        }
                    }
                }   //end of for loop
    
    
                 $tableName= str_replace('.txt','', $newFiles[$k]);
                 //echo $tableName."\n";
    
                //
                    //echo "\n".$dataType."\n";
                    $succ=mysql_query("CREATE TABLE `$tableName` ($dataType)");
                    if($succ)
                        echo "Table ".$tableName." was created Succesfully \n";
    
                    //echo $databaseName;
    
                     $loadsql = 'LOAD DATA LOCAL INFILE "'.$newFileLocation.$newFiles[$k].'" INTO TABLE `'.$tableName.'` FIELDS TERMINATED BY ","  IGNORE 1 LINES ('.$firstLine.')';
                     mysql_query($loadsql);     
    
    
    
    
            }//end of for loop
    
    
         mysql_close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would just like your feedback on something. Basically I have a value called
I would really like your help in knowing the procedure to delete a .3gp
I have one query on which I would like to get your valuable feedback.
I have a class that I would like to reference in my header file,
First I would like to thank you guys for all of your help with
I would like your guidance on how to create classes and their relationships (generalization,
I would like your advice about how best to solve my problem. In a
As I am new to this I would like your advice. So far I
I would like to know your experience when you need to take over somebody
I would like to get your opinion on as how far to go with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.