Compare commits

...

7 Commits

Author SHA1 Message Date
Sadeeq Aliyu
3b7b08e75f Merge 2f1e798f54 into 2aee46f169 2024-05-23 02:37:52 -03:00
Julien Le Coupanec
2aee46f169 Merge pull request #381 from FabricioLopees/fix/java
Update readme: fix grammatical error in java.md
2024-04-15 10:53:51 +02:00
FabricioLopees
22977d5ca1 update readme: fix grammatical error in java.md 2024-04-13 13:05:38 -03:00
FabricioLopees
a2c99c9674 update readme: fix grammatical error in java.md 2024-04-13 12:52:16 -03:00
Bgumel
2f1e798f54 PDO class
PDO class to connect  and query database faster
2018-09-28 16:25:31 +02:00
Bgumel
d0f3d05919 new list (cheet class) added 2018-09-28 16:21:04 +02:00
Bgumel
bef420a611 PDO cheat class 2018-09-28 16:20:22 +02:00
3 changed files with 97 additions and 1 deletions

View File

@@ -95,6 +95,14 @@ Feel free to take a look. You might learn new things. They have been designed to
- [Redis](databases/redis.sh)
</details>
#### Cheat Class
<details>
<summary>view cheatsheets</summary>
- [PDO](cheatclass/PDO.php)
</details>
### 🔧 Tools
<details>

88
cheatclass/PDO.php Normal file
View File

@@ -0,0 +1,88 @@
<?php
/*
* PDO DATABASE CLASS
* Connects Database Using PDO
* Creates Prepeared Statements
* Binds params to values
* Returns rows and results
* credit: Traversy media
*/
class Database {
private $host = "localhost";
private $user = "DB_User";
private $pass = "DB_PASS";
private $dbname = "DB_NAME";
private $dbh;
private $error;
private $stmt;
public function __construct() {
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array (
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try {
$this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
} // Catch any errors
catch ( PDOException $e ) {
$this->error = $e->getMessage();
}
}
// Prepare statement with query
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
// Bind values
public function bind($param, $value, $type = null) {
if (is_null ($type)) {
switch (true) {
case is_int ($value) :
$type = PDO::PARAM_INT;
break;
case is_bool ($value) :
$type = PDO::PARAM_BOOL;
break;
case is_null ($value) :
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute the prepared statement
public function execute(){
return $this->stmt->execute();
}
// Get result set as Associative array
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Get single record as object
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
// Get record row count
public function rowCount(){
return $this->stmt->rowCount();
}
// Returns the last inserted ID
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
}

View File

@@ -286,7 +286,7 @@ for(dataType item : array) {
### ACCESS MODIFIERS
1. defualt(No keyword required)
1. default(No keyword required)
2. private
3. public
4. protected