Database Connectivity In PHP With Example
Last Updated : Mar 11, 2024
In this article we will show you the solution of database connectivity in PHP with example, the mysqli constructor mysqli_connect() function is available in PHP to open a database connection.
This function accepts six arguments and, in the event of success, returns a MySQL link identifier; otherwise, it returns FALSE.
A connection between a MySQL database and a PHP script must be established before you can access or add anything to the database.
This article explains how to connect with MySQL using the PHP Data Objects and MySQLi Extension.
The conventional, legacy mysql_ functions are deprecated, thus we won't discuss them in this manual.
Only MySQL databases are supported by the extension known as MySQLi.
It provides both an object-oriented and procedural interface, enabling access to new functionalities present in MySQL systems (version 4.1 and above).
It does not support client-side prepared statements, only server-side prepared statements.
We will now discuss the idea of database connectivity in php with example.
Step By Step Guide On Database Connectivity In PHP With Example :-
Code 1
<?php $servername = "localhost"; $database = "database"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo “Connected successfully”; mysqli_close($conn); ?>
Code 2
<html> <head> <title>TalkersCode MySQL Server</title> </head> <body> <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'root@123'; $mysqli = new mysqli($dbhost, $dbuser, $dbpass); if($mysqli->connect_errno ) { printf("Connect failed: %s<br />", $mysqli->connect_error); exit(); } printf('Connected successfully.<br />'); $mysqli->close(); ?> </body> </html>
- Declare the database credentials: $servername, $database, $username, and $password.
- Establish a connection to the database server using mysqli_connect() function, passing the servername, username, password, and database as parameters.
- Check if the connection is successful using the connect_error property of the $conn object. If the connection failed, display an error message using the die() function.
- If the connection is successful, display a success message using the echo() function.
- Utilize the mysqli_close() method to terminate the database connection.
Conclusion :-
As a result, we have successfully learned database connectivity in php with example. This post described two ways to connect with an MySQL database using PHP.
There are benefits to both MySQLi and PDO. You should keep in mind, nonetheless, that MySQLi is only employed with MySQL databases.
So, you will need to completely rewrite the code if you want to switch to a different database.
However, PDO is able to work with 12 different databases, which makes the migration much simpler.
I hope this article on database connectivity in PHP with example helps you and the steps and method mentioned above are easy to follow and implement.