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$host = "localhost";//Ip of database, in this case my host machine
2$user = "root"; //Username to use
3$pass = "qwerty";//Password for that user
4$dbname = "DB";//Name of the database
5
6try {
7 $connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
8 $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
9
10}catch(PDOException $e)
11{
12 echo $e->getMessage();
13}
1<?php
2class database{
3 private $host = "localhost";
4 private $db_name = "php_basic";
5 private $username = "root";
6 private $password = "";
7 private $conn;
8
9 // connect database using PDO
10 function connect_pdo(){
11 try{
12 $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db_name, $this->username, $this->password);
13 return $this->conn;
14 }
15 catch(PDOException $ex){
16 echo "Connection Error -->> ",$ex->getMessage();
17 echo "<br>Error Code -->> ",$ex->getCode();
18 echo "<br>Error occur in File -->> ",$ex->getFile();
19 echo "<br>Error occur on Line no -->> ",$ex->getLine();
20
21 $this->conn = null; // close connection in PDO
22 }
23 }
24}
25?>
26
27//how to use
28<?php
29include 'connect_db.php';
30$database=new database();
31$db = $database->connect_pdo();
32?>
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} catch(PDOException $e) {
12 echo "Connection failed: " . $e->getMessage();
13}
14?>
1$host = 'localhost'; // URL to the server
2$user = 'root'; // Username (xampp = root)
3$pw = ''; // Password (xampp = )
4$dbname = 'database'; // The name of your database
5$dsn = "mysql:host=$host;dbname=$dbname";
6$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']; // If you want to use utf-8 use this line
7
8$db = new PDO($dsn, $user, $pw, $options); // Database Object
9$db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Use this if you want an associate array
10// $db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); Use this if you want an indexed array
11
12$qry = 'select * from table;'; // Your query
13$result = $db -> query($qry); // execute query
14
15while ($row = $result -> fetch()) {
16 $id = $row[/*column-name*/];
17}