patternphpMinor
Database connection wrapper class
Viewed 0 times
databasewrapperclassconnection
Problem
I am trying to create a PHP / MySQLi wrapper class that uses prepared statements, the goal of the class is to create a connection to the Database, then
The class should hopefully be general enough to be able to use in multiple applications with other wrapper classes that call on this class with more specific query functions.
Please let me know what you think, and any way I could improve this class.
```
connect();
}
public function connect() {
// Try and connect to the database
if(!isset(self::$connection)) {
// get information from config file to connect to database
self::$connection = new mysqli('localhost', DB_USER, DB_PASS,DB_NAME);
}
// 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;
}else{
//echo message for testing purpose
echo ' connection successful' ;
}
return self::$connection;
}
/**
* Create a secure hash
*
* Creates a secure copy of the user password for storage
* in the database.
*
* @param string $password The user's created password
* @param string $nonce A user-specific NONCE
* @return string $secureHash The hashed password
*/
//TODO UPDATE TO USE PHP.DEFAULT HASH FUNCTIONS
function hash_password($password, $nonce) {
$secureHash = hash_hmac('sha512', $password . $nonce, SITE_KEY);
return $secureHash;
}
/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
public 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 $
INSERT / SELECT / UPDATE information from the database.The class should hopefully be general enough to be able to use in multiple applications with other wrapper classes that call on this class with more specific query functions.
Please let me know what you think, and any way I could improve this class.
```
connect();
}
public function connect() {
// Try and connect to the database
if(!isset(self::$connection)) {
// get information from config file to connect to database
self::$connection = new mysqli('localhost', DB_USER, DB_PASS,DB_NAME);
}
// 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;
}else{
//echo message for testing purpose
echo ' connection successful' ;
}
return self::$connection;
}
/**
* Create a secure hash
*
* Creates a secure copy of the user password for storage
* in the database.
*
* @param string $password The user's created password
* @param string $nonce A user-specific NONCE
* @return string $secureHash The hashed password
*/
//TODO UPDATE TO USE PHP.DEFAULT HASH FUNCTIONS
function hash_password($password, $nonce) {
$secureHash = hash_hmac('sha512', $password . $nonce, SITE_KEY);
return $secureHash;
}
/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
public 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 $
Solution
Formatting
The elephant in the room:
the formatting is really, really bad.
Haphazard spaces, indents, and inexplicable things like this:
Surely you can do better if you try.
Keep in mind that code is read far more often than it is written.
Favoring write-time convenience over read-time convenience is a false economy.
Connection
The connect method is called every time you execute a query, and it checks every time if a connection object exists and creates if necessary.
Why not do that in the constructor, only once,
And let the rest of the code simply reuse the connection object directly?
Simplify
Instead of this:
How about simply:
Pointless comments
Many of the comments are unnecessary.
Take for example:
That echo statement seems kinda self-explanatory ain't it...
The elephant in the room:
the formatting is really, really bad.
Haphazard spaces, indents, and inexplicable things like this:
/**
* Prepared Update statement
*
*
*
*
*
*
*/Surely you can do better if you try.
Keep in mind that code is read far more often than it is written.
Favoring write-time convenience over read-time convenience is a false economy.
Connection
The connect method is called every time you execute a query, and it checks every time if a connection object exists and creates if necessary.
Why not do that in the constructor, only once,
And let the rest of the code simply reuse the connection object directly?
Simplify
Instead of this:
if($stmt->affected_rows){
return true ;
}
return false ;How about simply:
return $stmt->affected_rowsPointless comments
Many of the comments are unnecessary.
Take for example:
//echo message for testing purpose
echo ' connection successful' ;That echo statement seems kinda self-explanatory ain't it...
Code Snippets
/**
* Prepared Update statement
*
*
*
*
*
*
*/if($stmt->affected_rows){
return true ;
}
return false ;return $stmt->affected_rows//echo message for testing purpose
echo ' connection successful' ;Context
StackExchange Code Review Q#95974, answer score: 2
Revisions (0)
No revisions yet.