Mysql PHP

How to Connect php with mysql Database

Code for mysql connection with php

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'dbname');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

1. In the below  we can deifne our host name. For local you can use localhost. If you want to acces the other host then put that host  name or Server IP here

define('DB_SERVER','localhost');

2.This code for db user name. For local we use root. But you can create  db user name whatever you want.

define('DB_USER','root');

3. Here we can define db password. For local you should leave blank.

define('DB_PASS' ,'');

4.Here we define our  database name

define('DB_NAME', 'dbname');

5. The mysqli_connect() function opens a new connection to the MySQL server.

$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

Recommended Tutorials for you


Related posts

Session In PHP

Anuj Kumar

PHP Variables

Anuj Kumar

MySQL Introduction

Anuj Kumar

MySQL Data Type

Anuj Kumar

PHP – PEAR

Anuj Kumar

MySQL GUI Tools

Anuj Kumar

MySQL Insert Into

Anuj Kumar

Password Hashing in PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.