Reroute getConnection() function in NoteModel.php to getConnectionWithMySQLI
This commit is contained in:
@@ -32,7 +32,36 @@ class DatabaseFactory
|
||||
return self::$factory;
|
||||
}
|
||||
|
||||
public function getConnection() {
|
||||
public function getConnectionWithMySQLI()
|
||||
{
|
||||
if (!$this->database) {
|
||||
// Throw exceptions and prevent also throwing credentials.
|
||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||
|
||||
try {
|
||||
$host = Config::get('DB_HOST');
|
||||
$user = Config::get('DB_USER');
|
||||
$pass = Config::get('DB_PASS');
|
||||
$name = Config::get('DB_NAME');
|
||||
$port = (int) Config::get('DB_PORT');
|
||||
$charset = Config::get('DB_CHARSET') ? Config::get('DB_CHARSET') : 'utf8mb4';
|
||||
|
||||
$this->database = new mysqli($host, $user, $pass, $name, $port);
|
||||
|
||||
// Set charset (important for security + correct encoding)
|
||||
$this->database->set_charset($charset);
|
||||
} catch (mysqli_sql_exception $e) {
|
||||
echo 'Database connection can not be estabilished. Please try again later.' . '<br>';
|
||||
echo 'Error code: ' . $e->getCode();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
if (!$this->database) {
|
||||
|
||||
/**
|
||||
@@ -46,7 +75,9 @@ class DatabaseFactory
|
||||
$this->database = new PDO(
|
||||
Config::get('DB_TYPE') . ':host=' . Config::get('DB_HOST') . ';dbname=' .
|
||||
Config::get('DB_NAME') . ';port=' . Config::get('DB_PORT') . ';charset=' . Config::get('DB_CHARSET'),
|
||||
Config::get('DB_USER'), Config::get('DB_PASS'), $options
|
||||
Config::get('DB_USER'),
|
||||
Config::get('DB_PASS'),
|
||||
$options
|
||||
);
|
||||
} catch (PDOException $e) {
|
||||
|
||||
@@ -59,6 +90,7 @@ class DatabaseFactory
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->database;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,14 +29,13 @@ class NoteModel
|
||||
*/
|
||||
public static function getNote($note_id)
|
||||
{
|
||||
$database = DatabaseFactory::getFactory()->getConnection();
|
||||
$database = DatabaseFactory::getFactory()->getConnectionWithMySQLI();
|
||||
|
||||
$sql = "SELECT user_id, note_id, note_text FROM notes WHERE user_id = :user_id AND note_id = :note_id LIMIT 1";
|
||||
$query = $database->prepare($sql);
|
||||
$query->execute(array(':user_id' => Session::get('user_id'), ':note_id' => $note_id));
|
||||
|
||||
// fetch() is the PDO method that gets a single result
|
||||
return $query->fetch();
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user