1<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5$dbname = "myDB";
6
7// Create connection
8$conn= mysqli_connect($servername,$username,$password,$dbname);
9// Check connection
10if (!$conn) {
11 die("Connection failed: " . mysqli_connect_error());
12}
13echo "Connected Successfully.";
14?>
1<?php
2
3$hostName = 'localhost';
4$userNmame = 'root';
5$password = '';
6$dbname = 'topproduct';
7$db_name = "mysql:host=$hostName;dbname=$dbname";
8$conn = new PDO($db_name,$userNmame,$password);
9
10if(!$conn){
11 echo 'Error database connection';
12}
13
1<?php
2///////////neha jaiswal/////
3/////set variable/////////
4$serve="localhost";
5$user="root";
6$password="";
7$db="cart_system";///database name
8///create connection with db////
9$conn=mysqli_connect($serve,$user,$password,$db);
10////check condition for connection fail or not
11 if ($conn) {
12 echo "connection success";
13 }else
14{echo "connection unsuccess";
15 }
16 ?>
1Just include this Temlate in other file using PHP Include/Require Keywords
2 And Make Connection In One Shot :)
3
4<?php
5
6 // echo "Welcome to Connecting of DB Tutorial!";
7 // echo "<br>";
8
9 // 1. PDO - Php Data Objects
10 // 2. MySQLi extension
11
12 // Set Connection Variable
13 $server = "localhost";
14 $username = "root";
15 $password = "";
16 $database = "test";
17
18 // Create A Connection
19 $con = mysqLi_connect($server, $username, $password, $database);
20
21 // Check For Connection
22 if(!$con){
23 die ("Connection Terminated! by Die() function". mysqLi_connect_error());
24
25 }
26 else {
27 echo "Connection Succefully Happened! <br>";
28 }
29
30
31 ?>
1<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5
6// Create connection
7$conn = new mysqli($servername, $username, $password);
8
9// Check connection
10if ($conn->connect_error) {
11 die("Connection failed: " . $conn->connect_error);
12}
13echo "Connected successfully";
14?>
1<?php
2$servername = "localhost";
3$username = "yourusername"; // For MYSQL the predifined username is root
4$password = "yourpassword"; // For MYSQL the predifined password is " "(blank)
5
6// Create connection
7$conn = new mysqli($servername, $username, $password);
8
9
10// Check connection
11
12 if ($conn->connect_error) {
13
14 die("Connection failed: " . $conn->connect_error);
15}
16
17echo "Connected successfully";
18
19?>