Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <?php
  2. namespace App;
  3. class DB {
  4.  
  5. /* Database Configuration */
  6. public const HOST = "localhost";
  7. public const PORT = "3307";
  8. public const DB_NAME = "kaplash";
  9. public const USERNAME = "root";
  10. public const PASSWORD = "";
  11.  
  12. // PDO
  13. public static $pdo;
  14.  
  15.  
  16. public $query = "";
  17. public static $tableName = "";
  18.  
  19. public static function con ()
  20. // Start PDO Connection
  21. {
  22. try {
  23. self::$pdo = new \PDO("mysql:host=" . self::HOST . ":" . self::PORT . ";dbname=" . self::DB_NAME, self::USERNAME, self::PASSWORD);
  24. } catch ( \PDOException $e ) {
  25. echo $e->getCode(": ").$e->getMessage();
  26. }
  27. }
  28.  
  29.  
  30.  
  31.  
  32.  
  33. /* CREATION OF QUERY */
  34. public function raw ( string $query )
  35. // Take raw query. Still needs to be binded to.
  36. {
  37. $this->query .= $query;
  38. return $this;
  39. }
  40.  
  41. public static function table ( string $tableName )
  42. // Declare table name
  43. {
  44. self::con();
  45. self::$tableName = $tableName;
  46. return new static;
  47. }
  48.  
  49. public function select ( ...$columns )
  50. // Select with rows.
  51. {
  52. $string = ""; // This will be the string form of $columns
  53.  
  54. // Assume it's * if there isn't any arguments
  55. if(empty($columns))
  56. $string = "*";
  57. // Implode the array into `column` format
  58. else
  59. $string = '`'.implode('`,`', $columns).'`';
  60.  
  61.  
  62. $this->query .= "SELECT " . $string . " FROM " . '`' . self::$tableName . '`';
  63. return $this;
  64. }
  65.  
  66. public function update ( array $updates )
  67. // Update Something
  68. {
  69. foreach ( $updates as $column => $value )
  70. {
  71. $this .= "";
  72. }
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. /* QUERY COMPLETED. FURTHER PDO STUFF */
  80. public function prepare ()
  81. // Prepare the query.
  82. {
  83. $this->query = self::$pdo->prepare($this->query);
  84. return $this;
  85. }
  86.  
  87. public function bind ( $binds, $val = false )
  88. // Bind the parameters. Takes full array of all binds.
  89. {
  90. if ( is_array($binds) )
  91. // If this is an array, we can assume it looks like ['name' => 'val']
  92. {
  93. foreach ($binds as $name => $val)
  94. {
  95. $this->query->bindParam($name, $val);
  96. }
  97. } elseif( !is_array($binds) && $val ) {
  98. // If it isn't an array, and $val is set, it must be something like ->bind('name', 'val')
  99. $this->query->bindParam($binds, $val);
  100. }
  101.  
  102. return $this;
  103. }
  104.  
  105. public function execute ()
  106. // Execute the query. This is the final stage, returns a PDO object.
  107. {
  108. try {
  109. $this->query->execute();
  110. } catch ( \PDOException $e ) {
  111. echo $e->getCode(": ").$e->getMessage();
  112. }
  113. return $this->query;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement