How to Connect to Mysql Database with PHP

Add Tool

Connect to Mysql Database with PHP

Connect to Mysql Database with PHP

To work with Database you need Connect to Mysql Database. Here we will learn "How to Connect to Mysql Database with PHP coeds". The codes below are the default codes to connect with MySQL.

$con=mysql_connect("localhost" , "root" , "");
if (!con)
die('Could not connect:' .mysql_error());

echo "Connected successfully";

The main script

$con=mysql_connect("localhost" , "root" , "");


Localhost Servername
Root Username
" " Password area
die('Could not connect:' .mysql_error());

If don’t connect Could not connect message will be shown.

echo "Connected successfully";

If connect successfully with MySQL, it will show Connect successfully.

Where I place the codes?

These codes will be placed within body tag <body>here connect codes</body>. You can add this Mysql connect codes at the top of this page or you can create another page and call the page with include or require php tags.

Suppose you have created a mysql connection page as connect.php. Now we will add this connection page with our targeted page. So open your targeted page and add the below code at the top of the tage

<?php require_once ('connect.php'); ?>

See the below codes where I have added the above codes

<?php require_once ('connect.php');?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style.css"/>

For details see site

Tags: connect to mysql database, mysql connect to database, mysql connect to remote database, how to connect to mysql database, connect to mysql database php

About Masum Billah

Connect to Mysql Database with PHP 1stWebSuite website seo tool is a free website ranking tool site provides a lot of on page and off page seo useful tools for you. On this site, we have a large number of website ranking tools


Discuss about: How to Connect to Mysql Database with PHP