DEV Community

Cover image for mysql database connection in php
pavankumarsadhu
pavankumarsadhu

Posted on • Updated on <time datetime="2021-09-02T17:29:03Z" class="date-no-year">Sep 2</time>

mysql database connection in php

Hi guys,

Today, we will see about mysql database connection in php.

In the world of web development, data plays a key role. To give best experience to the users, we need to keep track of user steps to make comfortable. Definitely we need to store data related to users.

<?php
$servername = "localhost"; //your servername
$username = "username"; // your db username
$password = "password"; // your db password
$database_name = "database_name"; // your db name

// connect to the server
$conn = new mysqli($servername,$username,$password,$database_name);

//check whether it is connected or not.
if($conn->connect_error)
{
echo "Not Connected";
}
else
{
echo "Successfully Connected";
}
?>
Enter fullscreen mode Exit fullscreen mode

Read: How to Increase upload limit upto 1000GB in Wordpress using Increase Upload Limit Plugin for LIFETIME Free of Cost?

In this way, we will connect to the MySQL Server using PHP.

Thanks for taking your valuable time for reading my post, if you have any queries , please leave a comment and will respond. If you find this post useful, please share it.

Social Profile : LinkedIn

Have a Nice Day!

Discussion (2)

Collapse
arxeiss profile image
Pavel Kutáč

Hi, rather than mysqli, I would prefer using PDO. php.net/manual/en/pdo.connections.php

It would be much easier to migrate from MySQL to another DB if needed. It is used in all frameworks like Laravel, Symfony etc.

Collapse
pavankumarsadhu profile image
pavankumarsadhu Author

Hi there,
Yeah I agree with you, but for beginners that will be little bit confusion, as a newbie, the above approach will be much simpler.

Thanks for your response!