Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App;
- class DB {
- /* Database Configuration */
- public const HOST = "localhost";
- public const PORT = "3307";
- public const DB_NAME = "kaplash";
- public const USERNAME = "root";
- public const PASSWORD = "";
- // PDO
- public static $pdo;
- public $query = "";
- public static $tableName = "";
- public static function con ()
- // Start PDO Connection
- {
- try {
- self::$pdo = new \PDO("mysql:host=" . self::HOST . ":" . self::PORT . ";dbname=" . self::DB_NAME, self::USERNAME, self::PASSWORD);
- } catch ( \PDOException $e ) {
- echo $e->getCode(": ").$e->getMessage();
- }
- }
- /* CREATION OF QUERY */
- public function raw ( string $query )
- // Take raw query. Still needs to be binded to.
- {
- $this->query .= $query;
- return $this;
- }
- public static function table ( string $tableName )
- // Declare table name
- {
- self::con();
- self::$tableName = $tableName;
- return new static;
- }
- public function select ( ...$columns )
- // Select with rows.
- {
- $string = ""; // This will be the string form of $columns
- // Assume it's * if there isn't any arguments
- if(empty($columns))
- $string = "*";
- // Implode the array into `column` format
- else
- $string = '`'.implode('`,`', $columns).'`';
- $this->query .= "SELECT " . $string . " FROM " . '`' . self::$tableName . '`';
- return $this;
- }
- public function update ( array $updates )
- // Update Something
- {
- foreach ( $updates as $column => $value )
- {
- $this .= "";
- }
- }
- /* QUERY COMPLETED. FURTHER PDO STUFF */
- public function prepare ()
- // Prepare the query.
- {
- $this->query = self::$pdo->prepare($this->query);
- return $this;
- }
- public function bind ( $binds, $val = false )
- // Bind the parameters. Takes full array of all binds.
- {
- if ( is_array($binds) )
- // If this is an array, we can assume it looks like ['name' => 'val']
- {
- foreach ($binds as $name => $val)
- {
- $this->query->bindParam($name, $val);
- }
- } elseif( !is_array($binds) && $val ) {
- // If it isn't an array, and $val is set, it must be something like ->bind('name', 'val')
- $this->query->bindParam($binds, $val);
- }
- return $this;
- }
- public function execute ()
- // Execute the query. This is the final stage, returns a PDO object.
- {
- try {
- $this->query->execute();
- } catch ( \PDOException $e ) {
- echo $e->getCode(": ").$e->getMessage();
- }
- return $this->query;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement