How to PHP MYSQL connection using cPanel Hosting


A website cannot be live without a live hosting server. You should have Cpanel hosting to live your website. Users can visit the live website from anywhere in the World. The most important thing is to go live with the best cPanel hosting. In this tutorial, we will learn how to make a PHP MYSQL connection using cPanel hosting. Here, we will create an MYSQL database and database user with a password using Cpanel hosting. You will need a host, database name, database username, and user password to connect PHP scripts to the MYSQL database. First of all, you have to learn how to connect the HTML page to the MYSQL database using PHP. The connection process will be the same as connecting the PHP MYSQL database on localhost. There is only a small difference to making a PHP MYSQL connection on a live server. 

How to make PHP MYSQL connection using Cpanel Hosting – 

First of all, you should have Cpanel Hosting. CPanel web hosting is reliable, easy to use, and provides all the tools you’ll require to run your website seamlessly. If you already completed your project in PHP on localhost then upload your source zip (Compress your source in a zip on your local computer) on your domain Cpanel->file manager in the public_html folder or any other folder in the public_html folder. The public_html is known as the root folder of a website. Extract the zip file to the folder where you want to access it. In the root folder or other. 
If you want to move your website from a local server to a live server and want to connect the MYSQL database to PHP scripts then follow these steps – 

1. Find MYSQL databases in your Cpanel –

 Login to your Cpanel. You can log in like yourdomain.com/cpanel or visit your hosting service provider account and manage hosting. You can find Cpanel there. Go to the Cpanel and find the MYSQL databases in the databases section (Hints- Scroll down, check in the left sidebar or search “database”). You will see the MYSQL databases option and MYSQL databases Wizard. Search is the best way to find MYSQL databases. Kindly click on MYSQL databases. 

PHP MYSQL database connection using cpanel hosting

2. Create a new database –

In this step, you have to create a new database name. You can give any database name. Write the database name and click on create database button. You will get a success message and a go-back link. Kindly click on go back link after the database is created successfully. 

Create MYSQL database using Cpanel hosting

3. MYSQL Users(Add New User) –

Scroll down the page and find the MYSQL user, Add New User form. You can search by Ctr+f in your browser. In this form, create a user, and enter your strong password containing special characters, and small and capital alphabets. If a password generator is available, create a password by password generator and copy and paste in both text boxes (password and password again). After that, click on create user button. You will get a successful user-created message. Click on go back. 

Add new user using Cpanel hosting

4. Add user to database –

In this step, you have to add a user to the database. Scroll down the page or find Add user to database form. In this form, select your user and database and click on add button. 

Add user to database using Cpanel hosting

5. Manage User Privileges –

After clicking the add button, you will redirect to a new page “Manage User Privileges”. Kindly tick on the All PRIVILEGES checkbox and click on the make changes button. Scroll down the page for the make changes button. After this process is submitted, click on go back link .

set mysql database all privileges using- panel hosting


6. Current Databases –

Find current databases in MySQL Databases and you can see the database and user. 

Current database in Cpanel hosting | Create database using Cpanel

Now, we have all details. We will use these details to make a PHP MYSQL connection using Cpanel
Database Name - bluetswb_mydata (your database name )
Database Username - bluetswb_myuser (your username )
User Password – cFgW(rX5sjwV (Your created password )
Host – localhost  (the host will be localhost for live and local server)
The host will be the same for the live and local servers.

7. Import database (phpMyAdmin) –

Now, go back to your Cpanel from where did you search MYSQL databases. Now , find phpmyadmin . Scroll down the page or search PHPMyAdmin. Click on PHPMyAdmin. Click on your database name from the left sidebar. Here, we will import the database. If you don’t have a MySQL database check in your local server PHPMYADMIN and export from there. You will get a .sql file after exported or in your source. Click on import on your Cpanel PHPMyAdmin panel. After that, Choose your file (for example – mydatabse.sql ) from the local computer and click on the Go button. Scroll down the page and you will get a Go button at the end of the page. You will get a successful message after the database is imported. In this way, you can export the database from the localhost(local server ) and import it to live server Cpanel hosting.

Import database in PHPMYADMIN using Cpanel hosting

8. Set your database credentials in your connection file –

If you create a PHP project, you create a connection file like – config.php file, pdo-config.php, db.php, etc. Using this file, you can make a PHP MYSQL connection. 
For example – 
config.php 

<?php session_start();
$databaseHost='localhost';
$databaseUser='bluetswb_myuser'; 
$databasePassword=' cFgW(rX5sjwV'; 
$databaseName='bluetswb_mydata';
try {
  $conn = new PDO("mysql:host=".$databaseHost.";dbname=".$databaseName, $databaseUser, $databasePassword);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo 'Database Connected Successfully'; 
} catch(PDOException $error) {
  echo "Something went wrong " . $error->getMessage();
}
?>


You can set your credentials like that.

How to insert data in the MYSQL database using PHP and Cpanel hosting? 

The insertion operation is the part of CRUD application in PHP. In this tutorial, we will insert data in the MYSQL database using PHP programming and Cpanel hosting (Live server, Live site). If you are looking for that how to insert data in the MYSQL database in a live server then follow the steps – 
1. Create a database table – Go to Cpanel->phpmyadmin->select your database->click on SQL 
We will create a table using the query below- 

CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `title` varchar(255) DEFAULT NULL,
  `price` double NOT NULL
);

Execute this query. 
2. Create a connection file – Go to Cpanel->file manager->public_html and create a config.php file. We will use a PHP PDO connection. 
config.php

<?php session_start();
$databaseHost='localhost';
$databaseUser='bluetswb_myuser'; 
$databasePassword='cFgW(rX5sjwV'; 
$databaseName='bluetswb_mydata';
try {
  $conn = new PDO("mysql:host=".$databaseHost.";dbname=".$databaseName, $databaseUser, $databasePassword);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  //echo 'Database Connected Successfully'; 
} catch(PDOException $error) {
  //echo "Something went wrong " . $error->getMessage();
}
?>

In the above connection file, you can set your live server credentials.
3.HTML Insert form in PHP – Now, create an index.php file in your public_html (root folder).In this file, we will create an HTML form with two text boxes and a submit button. We will insert HTML form data in the same index file using PHP scripts. 
index.php 

<?php require_once("config.php");?> 
<!DOCTYPE html>
<html>
<head>
    <title>Insert data in MYSQL database using PHP and Cpanel Hosting</title>
</head>
<body>
    <?php
    if(isset($_POST['submit_form']))
    {
        $title=$_POST['title']; 
        $price=$_POST['price']; 
                $stmt = $conn->prepare('INSERT into products(title,price) VALUES(:title,:price)') ;
            $res=$stmt->execute(array(
                    ':title' => $title,
                    ':price' => $price
                ));

              if($res)
              {
                echo 'Data inserted successfully';
              }         
    } 
    ?> 
    <form action="" method="POST">
        Product Title:- 
        <input type="text" name="title"  required><br> 
        Product Price 
        <input type="number" name="price" required><br> 
        <input type="submit" name="submit_form">
    </form>
</body>
</html>

Display data from the MYSQL database table in HTML table using PHP and Cpanel Hosting - 

You can display data from the MYSQL database using PHP and Cpanel Hosting. 
Create a products.php file in the public_html folder and display data using the SELECT query in PHP. 

products.php

<?php require_once('config.php'); ?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Display data from the database in HTML table using PHP and Cpanel Hosting</title>
</head>
<body>
<?php
 $stmt = $conn->query('SELECT * FROM products ORDER BY id DESC');
$rows=$stmt->fetchAll();
?>
    <table width='30%' border=1>
            <td>Product Name</td>
            <td>Product Price </td> 
        </tr>
        <?php 
        foreach($rows as $row) {         
            echo "<tr>";
            echo "<td>".$row['title']."</td>";
            echo "<td>".$row['price']."</td>"; 
                    
        }
        ?>
    </table>
</body>
</html>

If you want to execute an index file then just open your domain on your browser like – example.com 
Insert some data using the insert form. If you want to check errors on your live website then go to your Cpanel and find the PHP configuration or PHP version or MultiPHP Manager. You can set your PHP version here and also enable logo errors or display errors by configuration. 
If you want to display all the data from the MYSQL database then open a URL like – 
example.com/products.php 
In this way, you can make a PHP MYSQL connection using Cpanel Hosting.
This tutorial will help you with these questions-
1. Move the PHP website from localhost to the live server. 
2. PHP MYSQL connection using Cpanel hosting. 
3. PHP MYSQL connection on live server. 
4. How to create a live website using PHP. 
5. How to connect HTML page to MYSQL database using PHP and live site. 

Tips – 1. Must set your PHP version. You can find the PHP version in the PHP configuration or the PHP version or MultiPHP Manager option in your Cpanel. You can search and set your PHP version. 
2. If you move the localhost website to a live server using Cpanel then kindly check your localhost PHP version and set the same version on your live server (Cpanel hosting). 
3. You can display errors or enable an error log file from the PHP configuration options. Search PHP configuration or PHP version or MultiPHP INI Editor. 
4. In some hosting Cpanel, you can change the PHP version from - MultiPHP Manager or PHP Version. 
5. You can add a comment for any issues or help. We will reply to you soon as possible.


Please Share

Recommended Posts:-


Live Chat

Hello! How can we assist you today?