How to Create Database Using Mysql,Mysqli,PDO or using Oops concepts in php?


 Database Using Mysql,Mysqli,PDO or using Oops concepts in php?


99knowledgehub-connect-mysql-with-php
mysqli could be a additional for the mysql functions, with object-oriented and procedural sorts. it's support for prepared statements. PHP data Objects(PDO) could be a general database construct layer with support for MySQL between several other databases.

1. The mysql functions are practical and use manual escaping.
2. MySQLi is a additional for the mysql functions, with object-oriented and procedural versions. It has support for prepared statements.
3. PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases.

Database Connection Using Mysql: 

<?php
$con=mysql_connect("localhost","root","password");
if(!$con)
{
//die('could not connect:' mysql_error();
}
$db=mysql_select_db("dbname",$con);
if(!$db)
{
die("Error in Database Connection");
}
?>

Database Connection Using Mysqli: 

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database="dbname";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Database Connection Using PDO: 

<?php
$servername = "localhost";
$username = "root";
$password = "password";
global $DB;
try {
$DB = new PDO("mysql:host=$servername;dbname=dbname", $username, $password);
//$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// set the PDO error mode to exception
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

Database Connection Using Oops Concept: 

<?php
define('db_host', 'localhost');
define('db_user', 'root');
define('db_pass', 'password');
define('db_name', 'dbname');
class db_connect{
public $host = db_host;
public $user = db_user;
public $pass = db_pass;
public $dbname = db_name;
public $conn;
public $error;
public function connect(){
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
if(!$this->conn){
$this->error = "Fatal Error: Can't connect to database" . $this->connect->connect_error();
return false;
}
}
}
?>

Comments

Popular posts from this blog

What is Permalinks How to Reset permalinks ?

WHY will we want DIGITAL MARKETING?