Reroute getConnection() function in NoteModel.php to getConnectionWithMySQLI

This commit is contained in:
2025-12-10 09:36:57 +01:00
parent 1a30c45d62
commit 9094b58b6d
2 changed files with 158 additions and 127 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
/**