Skip to content

Instantly share code, notes, and snippets.

@PintuKumarPal
Created November 27, 2013 09:27
Show Gist options
  • Save PintuKumarPal/7672977 to your computer and use it in GitHub Desktop.
Save PintuKumarPal/7672977 to your computer and use it in GitHub Desktop.
<?php
//Connect to database in php using class with constructor
class DBHandler
{
private $DB_HOST = "localhost"; //localhost
private $DB_USER = "root"; //root
private $DB_PASS = "pinturp1"; // ""
private $DB_NAME = "ShemaDB"; //test
public $mysqli;
public function __construct() {
// CONNECT TO THE DATABASE
$this->mysqli = new mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME);
if (mysqli_connect_errno()) {
throw new Exception("Unable to connect to the database. Error number: " . $this->mysqli->connect_errno);
}
$this->mysqli->set_charset("uft8");
}
public function __destruct() {
//We are done with the database. Close the connection handle.
$this->mysqli->close();
}
}
?>
<?php
include('classes/dbconnect.php');
$obj = new DBHandler();
/*$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";
// Execute query
if (mysqli_query($obj->mysqli,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($obj->mysqli);
}*/
$GetPersion = mysqli_query($obj->mysqli,"SELECT * FROM Persons");
// Execute query
if (!$GetPersion)
{
echo "Error found: " . mysqli_error($obj->mysqli);
}
echo 'Person Count---'. mysqli_num_rows($GetPersion);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment