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$servername = "localhost";
3$username = "username";
4$password = "password";
5
6try {
7 $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
8 // set the PDO error mode to exception
9 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
10 echo "Connected successfully";
11 }
12catch(PDOException $e)
13 {
14 echo "Connection failed: " . $e->getMessage();
15 }
16?>
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
2<?php
3$serverName = "serverName\\sqlexpress"; //serverName\instanceName
4
5// Since UID and PWD are not specified in the $connectionInfo array,
6// The connection will be attempted using Windows Authentication.
7$connectionInfo = array( "Database"=>"dbName");
8$conn = sqlsrv_connect( $serverName, $connectionInfo);
9
10if( $conn ) {
11 echo "Connection established.<br />";
12}else{
13 echo "Connection could not be established.<br />";
14 die( print_r( sqlsrv_errors(), true));
15}
16?>
17
18
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 ?>
1<?php
2
3function init_db()
4{
5 try {
6
7 $host = 'localhost';
8 $dbname = 'db_name';
9 $charset = 'utf8';
10 $user = 'root';
11 $password = '';
12
13 $db = new PDO("mysql:host=$host;dbname=$dbname;charset=$charset",
14 $user,
15 $password);
16
17 } catch (Exception $e) {
18 die('Erreur : ' . $e->getMessage());
19 }
20
21 return $db;
22}