PHP Freelancer

Freelance PHP Developer

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 !!

      
Plugin by: PHP Freelancer

Comments

11 Responses to “Mysql Tutorial – create table in database”

  1. SouthParkKenny
    March 10th, 2009 @ 7:42 am

    Great idea to make that tuts, but please …. remove advertisement from code!!!

  2. Rakshit Patel
    March 13th, 2009 @ 11:39 am

    yeah sure.

  3. Mysql database backup from PHP file | Online Hungama
    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. [...]

  4. abcphp.com
    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….

  5. 15 essential PHP and MySQL tutorials!
    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.. [...]

  6. bnc connectors
    September 17th, 2011 @ 8:15 pm

    bnc cables…

    [...]Mysql Tutorial – create table in database | Online Hungama[...]…

  7. swellendam accommodation
    September 19th, 2011 @ 6:23 pm

    swellendam bnb…

    [...]Mysql Tutorial – create table in database | Online Hungama[...]…

  8. What is the Best Diet for YOU?
    September 19th, 2011 @ 11:41 pm

    What is the Best Diet for YOU?…

    [...]Mysql Tutorial – create table in database | Online Hungama[...]…

  9. Lean Cuisine Diet
    September 20th, 2011 @ 5:35 pm

    Lean Cuisine Diet…

    [...]Mysql Tutorial – create table in database | Online Hungama[...]…

  10. Tarot Reading
    September 23rd, 2011 @ 3:11 am

    Tarot Reading…

    [...]Mysql Tutorial – create table in database | Online Hungama[...]…

  11. nlp
    October 4th, 2011 @ 3:46 pm

    singapore nlp cerficiation…

    [...]Mysql Tutorial – create table in database | Online Hungama[...]…

Leave a Reply





Freelance PHP Developer