add db postgre
This commit is contained in:
parent
74ff9fcc3a
commit
7b6cf42467
@ -1,10 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Model;
|
namespace App\Model;
|
||||||
|
|
||||||
|
use Shalex\Engine\DataBase;
|
||||||
|
|
||||||
class MusicModel
|
class MusicModel
|
||||||
{
|
{
|
||||||
static function getData()
|
static function getData()
|
||||||
{
|
{
|
||||||
|
$db = new DataBase();
|
||||||
|
$result = $db->getDataBase('SELECT * FROM top_chart');
|
||||||
|
var_dump($result);
|
||||||
$box = [];
|
$box = [];
|
||||||
if ($handle = opendir('./uploads')) {
|
if ($handle = opendir('./uploads')) {
|
||||||
while (false !== ($entry = readdir($handle))) {
|
while (false !== ($entry = readdir($handle))) {
|
||||||
|
101
engine/core/DataBase.php
Normal file
101
engine/core/DataBase.php
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Shalex\Engine;
|
||||||
|
|
||||||
|
define('DB_PERSISTENCY', 'true');
|
||||||
|
define('DB_SERVER', '127.127.126.49');
|
||||||
|
define('DB_USERNAME', 'music');
|
||||||
|
define('DB_PASSWORD', '123');
|
||||||
|
define('DB_DATABASE', 'music');
|
||||||
|
define('PDO_DSN', 'pgsql:host=' . DB_SERVER . ';dbname=' . DB_DATABASE);
|
||||||
|
|
||||||
|
|
||||||
|
class DataBase
|
||||||
|
{
|
||||||
|
|
||||||
|
private static $db;
|
||||||
|
|
||||||
|
// Clear the PDO class instance
|
||||||
|
public static function Close()
|
||||||
|
{
|
||||||
|
self::$db = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createTable(\PDO $db)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS top_chart (
|
||||||
|
id INT(4) serial PRIMARY KEY,
|
||||||
|
name varchar(100),
|
||||||
|
url varchar(100),
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function openDataBase()
|
||||||
|
{
|
||||||
|
if (!isset(self::$db)) {
|
||||||
|
// Execute code catching potential exceptions
|
||||||
|
try {
|
||||||
|
// $options = array(\PDO::ATTR_PERSISTENT => DB_PERSISTENCY, \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
|
||||||
|
self::$db = new \PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD);
|
||||||
|
// self::$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
|
// $this->createTable(self::$db);
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
// Close the database handler and trigger an error
|
||||||
|
self::Close();
|
||||||
|
trigger_error($e->getMessage(), E_USER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return self::$db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lastInsertId()
|
||||||
|
{
|
||||||
|
$db = $this->openDataBase();
|
||||||
|
return $db->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDataBase($string)
|
||||||
|
{
|
||||||
|
$db = $this->openDataBase();
|
||||||
|
$db->exec($string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setBasePrepare($query, $data)
|
||||||
|
{
|
||||||
|
$db = $this->openDataBase();
|
||||||
|
$stmt = $db->prepare($query);
|
||||||
|
$stmt->execute($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDataBase($string)
|
||||||
|
{
|
||||||
|
$db = $this->openDataBase();
|
||||||
|
$query = $db->query($string);
|
||||||
|
if ($query) {
|
||||||
|
return $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rowCount($query, $data)
|
||||||
|
{
|
||||||
|
$db = $this->openDataBase();
|
||||||
|
$stmt = $db->prepare($query);
|
||||||
|
$stmt->execute($data);
|
||||||
|
return $stmt->rowCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBasePrepare($query, $data)
|
||||||
|
{
|
||||||
|
$db = $this->openDataBase();
|
||||||
|
$stmt = $db->prepare($query);
|
||||||
|
$stmt->execute($data);
|
||||||
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user