Created
September 20, 2018 09:27
-
-
Save vishnusomanus/7fc9eef55d9e1bb8815dbccb669e23e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Main.php | |
.................................................................. | |
<?php | |
class Main | |
{ | |
private $_host = 'localhost'; | |
private $_username = 'root'; | |
private $_password = 'admin'; | |
private $_database = 'test'; | |
protected $connection; | |
public function __construct() | |
{ | |
if (!isset($this->connection)) { | |
$this->connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database); | |
if (!$this->connection) { | |
echo 'Cannot connect to database server'; | |
exit; | |
} | |
} | |
return $this->connection; | |
} | |
public function getData($query) | |
{ | |
$result = $this->connection->query($query); | |
if ($result == false) { | |
return false; | |
} | |
$rows = array(); | |
while ($row = $result->fetch_assoc()) { | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
public function execute($query) | |
{ | |
$result = $this->connection->query($query); | |
if ($result == false) { | |
echo 'Error: cannot execute the command'; | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public function delete($id, $table) | |
{ | |
$query = "DELETE FROM $table WHERE id = $id"; | |
$result = $this->connection->query($query); | |
if ($result == false) { | |
echo 'Error: cannot delete id ' . $id . ' from table ' . $table; | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public function escape_string($value) | |
{ | |
return $this->connection->real_escape_string($value); | |
} | |
} | |
?> | |
index.php | |
.................................................................. | |
<?php | |
include_once("Main.php"); | |
$db = new Main(); | |
$data = $db->getData('SELECT * FROM `test_table`'); | |
print_r($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment