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$servername = "localhost";
2$username = "username";
3$password = "password";
4
5// Create connection
6$conn = new mysqli($servername, $username, $password);
7
8// Check connection
9if ($conn->connect_error) {
10 die("Connection failed: " . $conn->connect_error);
11}
12echo "Connected successfully";
13
14
15Simplified
16
17$conn = mysqli_connect('localhost', 'username', 'password');
18$database = mysqli_select_db($conn, 'database');
1<?php
2 $user = "username";
3 $pass = "password";
4 $host = "host";
5 $dbdb = "database";
6
7$conn = new mysqli($host, $user, $pass, $dbdb);
8 if ($conn->connect_error) {
9 die("Connection failed: " . $conn->connect_error);
10}
11?>
1# MySql improved
2<?php
3 $mysqli = new mysqli("localhost", "username", "password", "dbname");
4 $result = $mysqli->query("SELECT lastname FROM employees");
5?>
6# Connection with PDO
7<?php
8 $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
9 $result = $myPDO->query("SELECT lastname FROM employees");
10?>
11# With PHP legacy functions:
12<?php
13 mysql_connect('localhost','username','password');
14 mysql_select_db("dbname");
15 $result = mysql_query('SELECT lastname FROM employees');
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?>