In this post you will learn how to connect Database with MYSQL and PDO and how to connect database with phpmyadmin it is easy to connection with database other then that.
Their are two definition to create connection with database 1. MYSQLI here "I" stand for improved and 2. PDO connection both are easy to use for connection database but the different is MYSQLI stand for only phpmyadmin however this extension was deprecated since 2012 while PDO stand for 12 different database connection again as i said that PDO will work 12 different database system.
I special recommend you to use PDO because if you are planing to change you website database in future version and want to switch to other database then PDO connection will make you process easy.
Here both connection with database are object-oriented, you can easy to find connection error solution from internet.
Here MYSQLI also offers a procedural for API.
Please follow the following process till end and make you database connection with you project.
Step 1 : Install XAMPP server
In this step we will install XAMPP for APACHE AND MYSQLI for the run php project we need a environment for the start or run the project so we need to install it, Ignore if you already install or your system already have apace base.
If you want to install XAMPP click here Xampp download, Once install the xampp click on apache and mysql button to start server.
I have already started my server you can click on APACHE AND MYSQL button and start the server.
Step 2 : Open the database in browser
Once start the server go to your browser and open the database here is the link follow the link and open you database in your browser http://localhost/phpmyadmin
Next! click on NEW in you sidebare database after click you will see the interface like below image.
you can see here one input with label create database, Please enter the database name what you want in next process this will be you database name.
After open enter the database name whatever you want to wise after entered the database name click on Go and create new database now you can see database which you entered the name in http://localhost/phpmyadmin.
Well now you created new database next we will create connection with database with our php application.
Step 3 : Make connection file
This root directory or where you installed xampp make a connection file C:\xampp\htdocs\connection.php
After create the connection.php file in your server root directory we will make connection with database which early we created a database in phpmyadmin.
<?php $servername = "localhost"; $username = "username"; $password = "password"; // if you set the password enter the // $password = ""; // if password not set // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>
If your XAMPP or PHP Version below 5 version then the connection with database littlebit diffrent.
<?php $servername = "localhost"; $username = "username"; $password = "password";// if you set the password enter the // $password = ""; // if password not set // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
Step 4 : PDO connection (optional)
If you wish to connect with PDO follow the example. as we disscussed the PDO connection we can use 12 diffrent database and wild scope for it and easy to process.
<?php $servername = "localhost"; $username = "username"; $password = "password";// if you set the password enter the // $password = ""; // if password not set try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $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(); } ?>
Step 5 : Explain Variable
Important note : If you set the password in you database then enter here else password variable will have blank
Timestamp convert to date format
Well ! Now you are connected with your database.
I hope you successfully connected you php application with your database.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Simple WhatsApp Messages Send Using Chat API In PHP
- How To Add Google reCAPTCHA v3 In HTML Form Based PHP/Laravel Website
- IOS Push Notification Send Example
- How to connect database with php
- Adding Watermark On Image In PHP Example
- Firebase Notification Send Example
- PHP Post Request cURL Example With Authentication
- How To Check Valid Integer Values Using PHP Function Example
- Firebase Dynamic Links Shorten URL PHP Function
- How To Add Google reCAPTCHA v2 In HTML Form Based PHP/Laravel Website