• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

PHP Creating MySQL Tables

SangLian

Revenue Diversification Expert
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
85
Likes
123
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
PHP Creating MySQL Tables

Before, I created a tutorial for PHP MySQL Database. For the follow-up tutorial, we are going to learn on How To Create MySQL Table In The Database. In the database, it will consist one or more tables. So, let’s start to create MySQL Table.

Note: All database table has a unique name.
Using MySQLi and PDO To Create MySQL Table.

We are going to use CREATE TABLE statement to make a table in MySQL.
  1. -- --------------------------------------------------------

  2. --
  3. -- Table structure for table `tbl_registration`
  4. --

  5. CREATE

    TABLE

    `tbl_registration`

    (
  6. `tbl_registration_id`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  7. `first_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  8. `middle_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  9. `last_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  10. `email`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  11. `contact_number`

    VARCHAR

    (

    100

    )

    NOT

    NULL
  12. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

So, we are going to create our table, name as “tbl_registration” and contain with “tbl_registration_id”, “first_name”, “middle_name”, “last_name”, “email”, “contact_number”.

Note: In each database table has an individual primary key. In the example table above, our primary key is “tbl_registration_id”. And it's value is unique.
Creating Database Table Using MySQLi (Object-Oriented)

  1. <?php
  2. $servername

    =

    "localhost"

    ;
  3. $username

    =

    "username"

    ;
  4. $password

    =

    "password"

    ;
  5. $dbname

    =

    "add_query_pdo"

    ;

  6. // Create connection
  7. $conn

    =

    new

    mysqli(

    $servername

    ,

    $username

    ,

    $password

    ,

    $dbname

    )

    ;
  8. // Check connection
  9. if

    (

    $conn

    ->

    connect_error

    )

    {
  10. die

    (

    "Connection Error. Check your syntax."

    .

    $conn

    ->

    connect_error

    )

    ;
  11. }

  12. // sql to create table
  13. $sql

    =

    "CREATE TABLE `tbl_registration` (
  14. `tbl_registration_id` int(11) NOT NULL,
  15. `first_name` varchar(100) NOT NULL,
  16. `middle_name` varchar(100) NOT NULL,
  17. `last_name` varchar(100) NOT NULL,
  18. `email` varchar(100) NOT NULL,
  19. `contact_number` varchar(100) NOT NULL
  20. )"

    ;

  21. if

    (

    $conn

    ->

    query

    (

    $sql

    )

    ===

    TRUE

    )

    {
  22. echo

    "Your Table tbl_registration Successfully Created!!!"

    ;
  23. }

    else

    {
  24. echo

    "Error creating table. Check the Syntax."

    .

    $conn

    ->

    error

    ;
  25. }

  26. $conn

    ->

    close

    (

    )

    ;
  27. ?>

Creating Database Table Using MySQLi (Procedural)

  1. <?php
  2. $servername

    =

    "localhost"

    ;
  3. $username

    =

    "username"

    ;
  4. $password

    =

    "password"

    ;
  5. $dbname

    =

    "add_query_pdo"

    ;

  6. // Create connection
  7. $conn

    =

    mysqli_connect

    (

    $servername

    ,

    $username

    ,

    $password

    ,

    $dbname

    )

    ;
  8. // Check connection
  9. if

    (

    !

    $conn

    )

    {
  10. die

    (

    "Connection Error. Check your syntax."

    .

    mysqli_connect_error

    (

    )

    )

    ;
  11. }

  12. // sql to create table
  13. $sql

    =

    "CREATE TABLE `tbl_registration` (
  14. `tbl_registration_id` int(11) NOT NULL,
  15. `first_name` varchar(100) NOT NULL,
  16. `middle_name` varchar(100) NOT NULL,
  17. `last_name` varchar(100) NOT NULL,
  18. `email` varchar(100) NOT NULL,
  19. `contact_number` varchar(100) NOT NULL
  20. )"

    ;

  21. if

    (

    mysqli_query

    (

    $conn

    ,

    $sql

    )

    )

    {
  22. echo

    "Your Table tbl_registration Successfully Created!!!"

    ;
  23. }

    else

    {
  24. echo

    "Error creating table. Check the Syntax."

    .

    mysqli_error

    (

    $conn

    )

    ;
  25. }

  26. mysqli_close

    (

    $conn

    )

    ;
  27. ?>

Creating Database Table Using PDO (PHP Data Objects)

  1. <?php
  2. $servername

    =

    "localhost"

    ;
  3. $username

    =

    "username"

    ;
  4. $password

    =

    "password"

    ;
  5. $dbname

    =

    "add_query_pdo"

    ;

  6. try {
  7. $conn

    =

    new

    PDO(

    "mysql:host=$servername

    ;dbname=$dbname

    "

    ,

    $username

    ,

    $password

    )

    ;
  8. // set the PDO error mode to exception
  9. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;

  10. // sql to create table
  11. $sql

    =

    "CREATE TABLE `tbl_registration` (
  12. `tbl_registration_id` int(11) NOT NULL,
  13. `first_name` varchar(100) NOT NULL,
  14. `middle_name` varchar(100) NOT NULL,
  15. `last_name` varchar(100) NOT NULL,
  16. `email` varchar(100) NOT NULL,
  17. `contact_number` varchar(100) NOT NULL
  18. )"

    ;

  19. // use exec() because no results are returned
  20. $conn

    ->

    exec

    (

    $sql

    )

    ;
  21. echo

    "Your Table tbl_registration Successfully Created!!!"

    ;
  22. }
  23. catch(

    PDOException $e

    )
  24. {
  25. echo

    $sql

    .

    "<br>"

    .

    $e

    ->

    getMessage

    (

    )

    ;
  26. }

  27. $conn

    =

    null

    ;
  28. ?>

Output:

1_64.png


So, this is it. You have one table in your database. And you can create more tables in your database. For my next tutorial, we are going to learn on PHP Insert Data In MySQL.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,501

355,784

355,791

Top