Question

I'd like to have a function that creates database-tables:

input:

  1. $tableName
  2. $tableFields (parameters array)

output: new DB TABLE


The two dimension array contains the fields detentions:

  • a[0][0] = firstName
  • a[0][1] = varchar[25]
  • a[1][0] = lastName
  • a[1][1] = varchar[30]

The output is a table named $tableName with two fields firstName and lastName.


I think that I can build a create query by looping the array, yet I don't know how to tell php to commit this command and actually create the DB table.

Était-ce utile?

La solution

function createTable( $tableName,$a) {
    global $con; //database connection
    $query = "CREATE TABLE $tableName ";
    $temp = "";
    for($i=0;$i<count($a);$i++) {
        $temp .= $a[$i][0] . " " . $a[$i][1] . ",";
    }
    if($temp!='') {
     $query .= substr($temp,0,-1);
     mysqli_query($con,$query);
    }
    else {
      print "No column names provided";
    }

}

Autres conseils

just have a look into the mysqli module of PHP: http://www.php.net/manual/de/book.mysqli.php

There you can create a connection to a database, perform queries etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top