PHP MySql Connect


PHP MySql Connect:– In order to store some relevant data into mysql database we need to connect php to database server.Like we are using xampp software package for php web development so here we get inbuilt mysql server. Hence there is no need for extra installation of mysql server.

But if you have already a mysql server running on your machine sometimes port conflict issue may be occur.The solution for this problem is to disable mysql server service from your computer.
So only the xampp package is enough to work with database.

Connection to MySql Database

PHP offers two different ways to connect to MySQL server: MySQLi (Improved MySQL) and PDO (PHP Data Objects) extensions.

While the PDO extension is more portable and supports more than twelve different databases, MySQLi extension as the name suggests supports MySQL database only. MySQLi extension however provides an easier way to connect to, and execute queries on, a MySQL database server.

In PHP you can easily do this using the mysqli_connect() function. All communication between PHP and the MySQL database server takes place through this connection. Here’re the basic syntaxes for connecting to MySQL using MySQLi and PDO extensions:

Example // How to connect to the mysql database

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "data";
// Create connection
$conn = new mysqli($servername, $username, $password);

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

Advertisements

Add Comment

📖 Read More