Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

This topic is locked

simple script that creates a table and two columns

Posted 03 Apr 2008 14:17:25
1
has voted
03 Apr 2008 14:17:25 Richard Baron posted:
Need a simple script that creates a table and two columns in mysql db.

Replies

Replied 07 Apr 2008 17:19:06
07 Apr 2008 17:19:06 Roddy Dairion replied:
This is the script i use. I've mod it to make it easier to use. The only part you have to care to for it create a the table is $table="tablename"; tablename to the tablename you want. And $fieldname=array("field1"=>"int AUTO_INCREMENT PRIMARY KEY","field2"=>"VARCHAR(25)"; you can add to it at will you just need to know what you are writting. Check mysql manual for this.
Don't forget to change the connection config part.
good luck.
<pre id=code><font face=courier size=2 id=code>
&lt;?php
//Connection config
$mysql_connection="localhost";
$mysql_username="root";
$mysql_password="";
$mysql_db="test";
//end

//start connections
mysql_connect($mysql_connection,$mysql_username,$mysql_password);
mysql_select_db($mysql_db);
//end



//function to create db
function create($tablename,$fieldname)
{
while(list($field,$type) = each($fieldname))
{
$fields=$field." ".$type.",";
}

$create=mysql_query("CREATE TABLE IF NOT EXISTS ".$tablename."(".rtrim($fields,',')."" or die ("Unable to create tablename ".$tablename);
}
//end

/*
Column content

COLUMN CONTENT
NOT NULL
UNIQUE
AUTO_INCREMENT
DEFAULT
PRIMARY_KEY

usage
int AUTO_INCREMENT PRIMARY KEY NOT NULL

*/

/*
Set fieldnames

e.g.

$fieldname=array("test1"=&gt;"int AUTO_INCREMENT PRIMARY KEY","test1"=&gt;"VARCHAR(25)";

*/
$table="tablename";
$fieldname=array("test1"=&gt;"int AUTO_INCREMENT PRIMARY KEY","test1"=&gt;"VARCHAR(25)";
create($table,$fieldname);
</font id=code></pre id=code>

Reply to this topic