Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to Create Tables in MySQL Database using PHP ??

user-image
Question ajoutée par Hafiz M Shahzad Mushtaq , Head Of Software Development Team , XiTCLUB Solutions
Date de publication: 2014/03/10
Maalik Muhamed
par Maalik Muhamed , Deputy Mill Manager , AZANIA GROUP OF COMPANYS

 Try out following codes to create a database: <?php

 

$dbhost = 'localhost:3036';

$dbuser = 'root';

$dbpass = 'rootpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{die('Could not connect: ' . mysql_error());} 

echo 'Connected successfully';

$sql = 'CREATE Database test_db';

$retval = mysql_query( $sql, $conn );

if(! $retval )

{die('Could not create database: ' . mysql_error());}

echo "Database test_db created successfully\\n";

mysql_close($conn);

?>

 

Once you estblish a connection with a database server then it is required to select a particular database where your all the tables are associated.

This is required because there may be multiple databases residing on a single server and you can do work with a single database at a time.

 

Abdul Mannan
par Abdul Mannan , Senior Software Engineer , Northbay Solutions

This should be the query you need to run in order to create a tabe using php.

 

CREATE TABLE tutorials_tbl(tutorial_id INT NOT NULL AUTO_INCREMENT,tutorial_title VARCHAR(100) NOT NULL,tutorial_author VARCHAR(40) NOT NULL,submission_date DATE,PRIMARY KEY ( tutorial_id ));

 

First parameter is the Field Name

Second parameter is the Type of data you need to save.

Third parameter is the Default Value of the field.

You may fourth parameter i.e Auto Increment in order to incremented it always when inserted into the database.

 

And in the end you need to specify the Primary key of that table.

Look at this link to read that in more detail. http://www.tutorialspoint.com/mysql/mysql-create-tables.htm

Utilisateur supprimé
par Utilisateur supprimé

Multi-Language Support UTF-8 Formate Mysql Table With PHP.

it will help a lot !!!

<?php

 

$dbhost = 'localhost:3036';

 

$dbuser = 'root';

 

$dbpass = 'rootpassword';

 

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

 

if(! $conn )

 

{die('Could not connect: ' . mysql_error());} 

 

echo 'Connected successfully';

 

$sql = 'CREATE Database test_db CHARACTER SET utf8 COLLATE utf8_general_ci';

 

 

$retval = mysql_query( $sql, $conn );

 

 

$sql = 'CREATE TABLE employee( '.       'emp_id INT NOT NULL AUTO_INCREMENT, '.       'emp_name VARCHAR(20) NOT NULL, '.       'emp_address  VARCHAR(20) NOT NULL, '.       'emp_salary   INT NOT NULL, '.       'join_date    timestamp(14) NOT NULL, '.       'primary key ( emp_id )) ENGINE=InnoDB DEFAULT CHARSET=utf8;'; 

mysql_query( $sql, $conn );

 

if(! $retval )

 

{die('Could not create database: ' . mysql_error());}

 

echo "Database test_db created successfully\\\\\\\\n";

 

mysql_close($conn);

?>

 

 

Muhammad Fahad  - Fayidah

Kamatchi Panneer Selvam
par Kamatchi Panneer Selvam , Software Developer , Comtel General Trading LLC

#connnect database

$link=mysql_connect("host","user","password");

$con=mysql_select_db("databasename",$link);

#create query whatever like "CREATE/SELECT/UPDATE/INSERT/DELETE"

$sql="CREATE TABLE TABLE_NAME (FIELD1 DATATYPE, FIELD2 DATATYPE, PRIMERY KEY FIELD1 )  ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;";

#excute query

mysql_query($sql);

mysql_close();

 

Mostafa Magdy
par Mostafa Magdy , Senior/Lead React Native Developer , RASAN Software House

Why dont you use google to get your answer? :?

Afsheen Atif
par Afsheen Atif

My answer could be helpful especially for newbies to Php/Mysql programming.

To create a table using Php you need

1.A database (A database holds one or more tables).

2.connection with this db.

If you are working on localhost(like wamp or xampp etc) then just copy paste the following  whole  script between <?php   ?> tags in your Php file say first_table.php,online your server and run first_table.php on any browser.You’ll get a table Persons  in a new database first_db.

<?php

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{die('Could not connect: ' . mysql_error());}

echo 'Connected successfully';

$sql = 'CREATE Database first_db';

$creat_db = mysql_query( $sql, $conn );

if(!$creat_db)

{die('Could not create database: ' . mysql_error());}

echo "Database first_db created successfully\\\\\\\\n";

 

//To create a table in this first_db you need to connect to the database first_db.Use the script:

$link=mysql_select_db("first_db",$conn);

 

//Now to Create table with fields  FirstName,LastName  and Age

$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";

if (mysql_query($sql))

  {

  echo "Table persons created successfully";

  }

else

  {

  echo "Error creating table: " . mysql_error($link);

  }

mysql_close($conn);

?>

If you have a database then remove following create table lines:

$sql = 'CREATE Database first_db';

$creat_db = mysql_query( $sql, $conn );

if(!$creat_db)

{die('Could not create database: ' . mysql_error());}

echo "Database first_db created successfully ";

 

and add your database name inplace of first_db in following code:

$link=mysql_select_db("first_db",$conn);

 

In case of any confusions please feel free to ask.

Cheers!

JAHABAR YUSUFF MOHD MUSTHABA
par JAHABAR YUSUFF MOHD MUSTHABA , Senior Software Engineer , ACubetech Solutions Pvt Ltd

<?php

$dbhost = 'localhost:3036';

$dbuser = 'root';

$dbpass = 'rootpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{

  die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

$sql = 'CREATE TABLE employee( '.

       'emp_id INT NOT NULL AUTO_INCREMENT, '.

       'emp_name VARCHAR(20) NOT NULL, '.

       'emp_address  VARCHAR(20) NOT NULL, '.

       'emp_salary   INT NOT NULL, '.

       'join_date    timestamp(14) NOT NULL, '.

       'primary key ( emp_id ))';

 

mysql_select_db('test_db');

$retval = mysql_query( $sql, $conn );

if(! $retval )

{

  die('Could not create table: ' . mysql_error());

}

echo "Table employee created successfully\\n";

mysql_close($conn);

?>

Mohammad Rashedul Alam
par Mohammad Rashedul Alam , Marketing Manager , Pran Rfl Group ( EWG)

https://www.youtube.com/watch?v=VeztEL4fVOM

Noor Khan
par Noor Khan , Technical Team Lead , Ammrys Studios

execute Query 

$sql = "CREATE TABLE MyTABLE(PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID),FirstName CHAR(15),LastName CHAR(15),Age INT)";

islam khalil
par islam khalil , Technical Manager , iCloudit

Please try to not invient the wheel ,

try to use already made LIBs or framework to do this .

 

Check : PECL , PEAR

 

Islam Nadim
par Islam Nadim , Security Operations Engineer , Orange Business Services

You can visit this site. It generate the SQL as well as the PHP Code for you. This can help you understand how to create the SQL Queries from PHP directly.

More Questions Like This