How to Connect to Database Using PHP

Introduction

You can create databases on 000webhost.com and PHP can be used to connect to those databases. This tutorial explains three different methods how that could be achieved. Most of the websites need to have at least one database and if you are developing a website from the beginning you will have to connect it to a database manually, here you can find how that could be done step by step.

What you’ll need

Before you begin this guide you’ll need the following:

Step 1 — Creating a PHP file

You need to create a PHP file (with .php extension) on your computer and upload it to the server. It’s just a simple new file created on your computer which has a .php extension. Here is a tutorial about doing that on Windows system using Notepad: Creating PHP file.

Step 2 — Uploading the file created

You should navigate to the file manager of 000webhost now and upload the file created. On your control panel press Upload files and then Upload files now.

Once you are redirected to the File Manager press the control buttons and select Upload files as provided in the screenshots below:

Then you need to select the necessary file on your computer and press Upload button:

Step 3 — Editing the PHP file

Now you need to edit the uploaded file. It can be done directly on the File Manager. Press the right mouse button on the uploaded file and select edit:

The file should be edited depending on the connection method. If you are interested in differences between these methods you can read more here

Method 1 — Connecting to the database using PDO (recommended)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    } catch(PDOException $e) {    
    echo "Connection failed: " . $e->getMessage();
    }
?>

Method 2 — Connecting to the database using mysqli (Object-Oriented)

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Method 3 — Connecting to the database using mysqli_connect (Procedural)

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";
?>

Step 4 — Making the necessary changes

On all the methods you need to edit three lines only because 000webhost uses localhost as the servername (servername should not be changed).

  • Username - The username assigned to the database. It can be found on your 000webhost control panel -> Manage database. Username is combined of your
    000webhost username and the name you created.

  • Password - a password which you set for your username of this database.

  • Database - The name of your database. It can be found on your 000webhost control panel -> Manage database. Database name is combined of your 000webhost username and the name of the database you created.

If you are not sure where to find these entries it can be done on your 000webhost.com control panel -> Manage Database:


DB Name and DB User are visible on the page and if you forgot the password it can be changed by clicking the Manage button as provided in the screenshot above.

Step 5 — Testing the PHP connection

Now you can test if there are any errors in the syntax of the file. If your file is uploaded to public_html directory simply enter your website address to the browser and add /filename.php at the end of it. For example yourwebsite.000webhostapp.com/phpconnect.php. If you get a message Connected successfully on the browser it means, that script works just fine and you can use it when connecting your website or a script to a database. If you get Connection failed: an error should appear on the browser which should help you identify the mistake which was made.

Conclusion

These are three simple methods to connect to a database using a PHP file. They can be used to test the connection to the database and if the connection works fine you can start using commands which create, edit or delete entries in your database. It can also help you identify if the issue is on your website or the database of the server when your website can’t connect to the database and gives you an error.

1 Like
No old cpanel ?where can i find my sql hostname to use in php files to connect to the database
Is there something wrong with mysql host: mysql8.000webhost.com
HTTP 500 ERROR while connecting database for first time
Unable to mysqli_connect()@mysql8 -- mysql8 STILL down?
Need Help in database error..!
Free 000webhostapp database connection
Is there something wrong with mysql host: mysql8.000webhost.com
Help getting my code to work
SQL Connection Refused
Connection failed: SQLSTATE[HY000] [1045] ProxySQL Error: Access denied for user 'id2379720_jhapz'@'2a02:4780:bad:f00d::c' (using password: YES)
Mysqli_querry Access Denied
Extension connection pdo
Database connectivity issue in codeigniter
Not connected to the database
My database shows 0mb
Access denied for user 'root'
Como trazer de mysqli para mysql
Lmp4ct34 using wrong username for database connection
I can't access my database in 000webhost
No hay conexion con base de datos
Ftp_put(): Disk full - please upload later
I want to let my website connect to my own hosted MySQL Database. How do i do that?
Problemas con el archivo php que hace la conexión a la BD
Config php ini file
Error with set data in database
Mysql connect a6937125
¿como probar la base de datos?
About the phpmyadmin
How to create a Database with PDO?
Why can I not access SQL?
Daxo Question MySQL
Please I'm having issues with redirecting and sql connction
SQL Connection failed "Warning: mysqli_connect(): (HY000/2002): Connection refused"
Trying to input text into database
Able to fetch data from the database but wont send any data to database (NO ERROR SHOWN)
Failed Log In to phpMyAdmin
What is the Database Connection Code? Please help Thank you
Database Troubleshooting Guide
DataBase connection through PhPAdmin;
Mysql_query() expects parameter
Could not connect: error HELP!
I can't connect localhost
Probleme In config.php
Probleme In config.php
SIM900 Arduino trying to post to database
What is the phpadmin password/What parameters do I put for my PDO object?