I have this code I use to connect to the database and get a thing from it secure, How can I make more secure??
class db {
// The database connection
protected static $connection;
/**
* Connect to the database
*
* @return bool false on failure / mysqli MySQLi object instance on success
*/
protected function connect() {
// Try and connect to the database
if(!isset(self::$connection)) {
// Load configuration there are defined in config.php
require_once('app/config/config.php');
self::$connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DBNAME);
}
// If connection was not successful, handle the error
if(self::$connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return false;
}
return self::$connection;
}
/*
Query the database
@param $query The query string
@return mixed The result of the mysqli::query() function
*/
private function query($query) {
// Connect to the database
$connection = $this->connect();
// Query the database
$result = $connection->query($query);
return $result;
}
/*
Fetch rows from the database (SELECT query)
@param $query The query string
@return bool False on failure / array Database rows on success
*/
public function select($query) {
$result = $this->query($query);
if($result === false) {
return false;
}
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
When I want infomation from the DB Example:
$db = new db();
$result = $db->select("SELECT * FROM users WHERE name = John");