Mysql Tutorial – create table in database
Posted on | March 8, 2009 | 11 Comments
Hello Friends,
If you are a learner (new) in PHP / Mysql and don’t know how to create table in Mysql database from php file than Here is the easiest and error-free way to create table in Mysql database.
<?php
///*** This file will help you to create table in database using PHP – Rakshit Patel **////include(“mysql-connection.php”);
//** Creating databse **//
$sql = “CREATE DATABASE testing”;
$res = mysql_query($sql);//** select database **//
mysql_select_db(‘testing’) or die(‘!! can not select database !!’);
//** Creating table **//
$sql = ‘CREATE TABLE test_table( ‘.
‘id INT NOT NULL AUTO_INCREMENT, ‘.
‘name VARCHAR(20) NOT NULL, ‘.
‘email VARCHAR(50) NOT NULL, ‘.
‘message TEXT NOT NULL, ‘.
‘PRIMARY KEY(id))’;$res = mysql_query($sql);
////////// OR create table from file //////////////
$file = ‘tablename.txt’;
$fp = fopen($file, ‘r’); // open file in read mode
$sql = fread($fp, filesize($file)); // read file
fclose($fp); // clode file$res = mysql_query($sql);
////////////////////////////////////
include(“mysql-connection-close.php”);
?>
Here first i have added mysql-connection.php file which will make connection with Mysql database. If you dont know how to make connection with Mysql database than click here.
You can create database by excuting query as per above or by just calling inbuilt function mysql_create_db(“databasename”).
After successfully creating database , Select that database using inbuilt function mysql_select_db(“databasename”) to create tables in that database.
There are two ways to create table from PHP file. One is by executing query as per above.
Other is if you have .txt back up file of table than just open that file in read mode , read file and take the data in one variable and execute the query as i have written above.
At last i have included mysql-connection-close.php file.
<?php ///*** This file will close MYSQL connection using PHP – Rakshit Patel **////
mysql_close($connection); // close mysql connection
?>
This is nothing but it will close connection which we have made earlier. Its not necessary but a sign of Good Programmer.
Cheers !!
Comments
11 Responses to “Mysql Tutorial – create table in database”
Leave a Reply




I am
March 10th, 2009 @ 7:42 am
Great idea to make that tuts, but please …. remove advertisement from code!!!
March 13th, 2009 @ 11:39 am
yeah sure.
March 21st, 2009 @ 3:08 am
[...] After selecting database , you need to execute the query as written above to take back-up or load data in to table using backup file . If you have not created table yet , than click here. [...]
March 6th, 2010 @ 9:40 am
Mysql Tutorial – create table in database | Online Hungama…
If you are a learner (new) in PHP / Mysql and don’t know how to create table in Mysql database from php file than Here is the easiest and error-free way to create table in Mysql database….
March 8th, 2010 @ 10:10 am
[...] Mysql Tutorial – create table in database If you are a learner (new) in PHP / Mysql and don’t know how to create table in Mysql database from php file than Here is the easiest and error-free way to create table in Mysql database. see more with an example.. [...]
September 17th, 2011 @ 8:15 pm
bnc cables…
[...]Mysql Tutorial – create table in database | Online Hungama[...]…
September 19th, 2011 @ 6:23 pm
swellendam bnb…
[...]Mysql Tutorial – create table in database | Online Hungama[...]…
September 19th, 2011 @ 11:41 pm
What is the Best Diet for YOU?…
[...]Mysql Tutorial – create table in database | Online Hungama[...]…
September 20th, 2011 @ 5:35 pm
Lean Cuisine Diet…
[...]Mysql Tutorial – create table in database | Online Hungama[...]…
September 23rd, 2011 @ 3:11 am
Tarot Reading…
[...]Mysql Tutorial – create table in database | Online Hungama[...]…
October 4th, 2011 @ 3:46 pm
singapore nlp cerficiation…
[...]Mysql Tutorial – create table in database | Online Hungama[...]…