Creating Database Connection With PHP MySQLi

How to Connect MySQL Database with PHP

Linking PHP and Database is one of the basic things in web programming. By connecting PHP and the database, we can interact and access the database. such as displaying data from the database, inputting data into the database, editing and deleting the database and anything related to the database.

Why use MySQLi not use MySQL ?

to respond to the emergence of PHP 8, the use of MySQL can no longer be used on versions of PHP 5 and above (PHP 8). so in PHP 5 and above no longer use mysql, but use mysqli

www.widiantocoder.my.id
Creating a New Database

Finished creating databases with the name 'Widi'. next
create a new folder with the name 'koneksi' in the Local Disk(C:) xampp - htdocs folder.
if you have created a folder with the name connection, then create it with the name koneski.php

Creating a New Database

Creating a New File


then write the following syntax in the connection.php file using the Notepap++ or Sublime Text application

<?php
$connection = mysqli_connect("localhost","root","","Widi");
// check connection
if (mysqli connect_error()){ echo "Database connection failed : " . mysqli_connect_error();
}
?>

Explanation 1

$connection = mysqli_connect("localhost","root","","Widi");

A. parameter 1 of our hostname, namely: localhost
B. parameter 2 username from our mysql, if the default is root.
C. parameter 3 password, but if you use default then the password in mysql is empty, so just tick ""
D. parameter 4 you simply type the name of the database that you created. our database is Widi, so write it according to the name of the database you created.

visit here : 

Explanation 2

// check connection
if (mysqli_connect_error()){
echo "Database connection failed:
 " . mysqli_connect_error();
}
The function of (mysqli_connect_error() is
aims to check and display our connection is connected to the database or not, but if it is not connected then,
echo "Database connection failed : " . mysqli_connect_error(); means an error occurred. please look again at your connection syntax, there may be an error in writing it

Run the connection file to the database

Open the Xampp application then click start on Apache and MySQL so that it changes to stop

XAMPP
XAMPP

then call the connection that was made earlier by typing this address http://localhost/koneksi/koneksi.php in your computer's browser application.
a blank page will appear, this means that the connection we made is already connected to the database.

Connected

connected
Connected

what if we want to know if our connection is not connected?

in the connection syntax we change the database name to "Abjad"
$connection = mysqli_connect("localhost","root","","Abcd");
in the connection syntax, the database name has been changed to "Abjad" then we call the connection again.
http://localhost/koneksi/koneksi.php

not connected
not connected
\
Warning: mysqli_connect(): (HY000/1049): Unknown database 'abjad' in C:\xampp\htdocs\koneksi\koneksi.php on line 3

Why did it happen

because before we never created a database with the name 'ajad', then mysqli_connect() will tell us something is wrong. if you want to reconnect to the database. you simply change the database name back to 'Widi' in the connection.php syntax in the connections folder.
thank you hopefully useful



Comments

Popular posts from this blog

Cara cepat belah layar (Split Screen) windows 10 di laptop atau PC, Terbaru 2021 !!!

Tutorial Membuat Aplikasi Catatan Perjalanan UKK RPL 2022 Paket 1 (Peduli Diri)| Dengan PHP dan MySQLi (Part VII)

Cara cepat copy paste data tabel excel ke word tanpa berantakan, Terbaru 2021 !!!