Skip to content

Spoke Right

Education for everyone

https://spokeright.com
Primary Menu
  • Home
  • Our Services
    • Translation
    • Interpretation
    • Transcription
    • Voice-Over
    • Proofreading
    • Content writing
  • Free Education
    • Learn Android Studio
    • Learn Python
    • Learn MongoDB
    • Learn MySql
    • Learn React
    • Montessori Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
    • TicTac
    • Checkers
    • Foosball
    • Billiards
    • Master Chess
    • Logic Game
  • Contact Us
Download Now
  • Home
  • Learn MySql
  • How to create database and table in MySQL in PHP
  • Learn MySql

How to create database and table in MySQL in PHP

Spoke Right December 30, 2022 3 min read

Create Database

PHP uses mysqli query() or mysql_query() function to create a MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure.

Syntax

$mysqli->query($sql,$resultmode)
Sr.No.Parameter & Description
1

$sql

Required – SQL query to create a MySQL table.

2

$resultmode

Optional – Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

Example

Try the following example to create a table −

Copy and paste the following example as mysql_example.php −

<html>
   <head>
      <title>Creating MySQL Table</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = '[email protected]';
         $dbname = 'TUTORIALS';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         
         if($mysqli->connect_errno ) {
            printf("Connect failed: %s<br />", $mysqli->connect_error);
            exit();
         }
         printf('Connected successfully.<br />');
         $sql = "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 )); ";
         if ($mysqli->query($sql)) {
            printf("Table tutorials_tbl created successfully.<br />");
         }
         if ($mysqli->errno) {
            printf("Could not create table: %s<br />", $mysqli->error);
         }
         $mysqli->close();
      ?>
   </body>
</html>

Output

Access the mysql_example.php deployed on apache web server and verify the output.

Connected successfully.
Table tutorials_tbl created successfully.

In this guide, we will discuss how to create a MySQL database and table using PHP and also how to create the MySQL/MariaDB databases and tables via phpMyAdmin in XAMPP stack.

Prerequisites

Make sure you have setup XAMPP stack in your system. The following guide explains how to setup XAMPP stack in Linux.

  • How To Install XAMPP In Linux

Alternatively, you can use the LAMP or LEMP stacks which provides both PHP and MySQL. If you’re on Linux, refer the following guides to install LAMP/LEMP stacks.

  • Install Apache, MySQL, PHP (LAMP Stack) On Ubuntu 20.04 LTS
  • Install Nginx, MySQL, PHP (LEMP Stack) On Ubuntu 20.04 LTS
  • Install Apache, MariaDB, PHP (LAMP Stack) In CentOS 8
  • Install Apache, MariaDB, PHP (LAMP) stack on Arch Linux
  • Install Nginx, MariaDB, PHP (LEMP) stack on Arch Linux

Setting up XAMPP is much easier than LAMP and LEMP stacks. So, we will will be using XAMPP stack throughout this guide.

Connect To MySQL Using PHP

1. Specify MySQL servername, username and password parameters in your PHP code.

Here, my servername is localhost, MySQL username is root and its password is empty.

2. Create a connection using the above details.

By using mysqli_connect() function, we will establish a connection. It will take three parameters. First will be the servername, second is the username (i.e. root) and last is the password. It will also take a database name which is optional here, because we are only creating the connection.

Code:

$connection = mysqli_connect($server_name, $user_name, $password);

3. Check the connection.

We can check the connection using the mysqli_connect_error() function specified in an if condition. This function will display an error, if the connection is failed.

Now, let us write a sample PHP code based on the above steps.

PHP Code To Connect To MySQL

Create a new PHP file named Connect_data.php under the /htdocs directory with the following contents in it.

Heads Up: If you use Linux, the htdocs folder will be under /opt/lampp/ directory. If you’re on Windows, the htdocs will be usually in C:\xampp\ folder.

<?php
//specify the server name and here it is localhost
$server_name = "localhost";

//specify the username - here it is root
$user_name = "root";

//specify the password - it is empty
$password = "";

// Creating the connection by specifying the connection details
$connection = mysqli_connect($server_name, $user_name, $password);

// Checking the  connection
if (!$connection) {
  die("Failed ". mysqli_connect_error());
}
echo "Connection established successfully";
?>

Heads Up: If you’re using LAMP/LEMP stacks, you may need to update the MySQL root password in the above code.

Open your web browser and point it to http://localhost/Connect_data.php URL. If you see the “Connection established successfully” message, you’re connected with XAMPP.

Continue Reading

Previous: How do Select Database Example in PHP & MySQL
Next: How to drop a table in MySQL using PHP

Related Stories

How to use database example in MySQL
2 min read
  • Learn MySql

How to use database example in MySQL

January 6, 2023
How is NULL value handled in MySQL give example https://spokeright.com/
2 min read
  • Learn MySql

How is NULL value handled in MySQL give example

January 6, 2023
How to use join query in PHP with example
1 min read
  • Learn MySql

How to use join query in PHP with example

January 3, 2023

This AD Will support Us

Support Us

Coding Ustad LTD Support
Coding Ustad LTD Support

Coding Ustad LTD

Coding Ustad LTD
Coding Ustad LTD

Recent Posts

  • How to Install Node.js and npm on CentOS 7
  • How to Install Node.js and npm on CentOS 7
  • How to Install CentOS Web Panel (CWP) on CentOS 7
  • The Power of Marketing in Real Estate: A Guide to Boost Your Business
  • Vehicle Verification in Pakistan

Get free Hosting

Hostens.com - A home for your website

You may have missed

How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 3, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 2, 2023
How to Install CentOS Web Panel (CWP) on CentOS 7
3 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install CentOS Web Panel (CWP) on CentOS 7

May 2, 2023
The Power of Marketing in Real Estate: A Guide to Boost Your Business https://thaikadar.com/secureinvestment/
2 min read
  • Article By Spoke Right
  • Blog By Spoke Right
  • Circular Byte Private Limited

The Power of Marketing in Real Estate: A Guide to Boost Your Business

January 30, 2023
  • Home
  • Our Services
  • Free Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
  • Contact Us
Copyright © All rights reserved. | MoreNews by AF themes.