├── .gitignore ├── Info.php ├── TestFetchAll.php ├── GetConnection.php ├── TestAutoIncrement.php ├── TestExecute.php ├── TestPrepareNonQuery.php ├── TestQuery.php ├── TestConnection.php ├── TestFetch.php ├── TestTransaction.php ├── TestPrepareIndexWithoutBind.php ├── TestSqlInjection.php ├── TestTransactionRollback.php ├── TestRepository.php ├── TestPrepareIndex.php ├── TestPrepare.php ├── Model └── Comment.php └── Repository └── CommentRepository.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /Info.php: -------------------------------------------------------------------------------- 1 | query($sql); 9 | 10 | $customers = $statement->fetchAll(); 11 | var_dump($customers); 12 | 13 | $connection = null; 14 | -------------------------------------------------------------------------------- /GetConnection.php: -------------------------------------------------------------------------------- 1 | exec("INSERT INTO comments(email, comment) VALUES ('eko@test.com', 'hi')"); 8 | $id = $connection->lastInsertId(); 9 | 10 | echo $id . PHP_EOL; 11 | 12 | $connection = null; 13 | -------------------------------------------------------------------------------- /TestExecute.php: -------------------------------------------------------------------------------- 1 | exec($sql); 13 | 14 | $connection = null; 15 | -------------------------------------------------------------------------------- /TestPrepareNonQuery.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 12 | $statement->bindParam("username", $username); 13 | $statement->bindParam("password", $password); 14 | $statement->execute(); 15 | 16 | $connection = null; 17 | -------------------------------------------------------------------------------- /TestQuery.php: -------------------------------------------------------------------------------- 1 | query($sql); 9 | 10 | foreach ($statement as $row) { 11 | $id = $row["id"]; 12 | $name = $row["name"]; 13 | $email = $row["email"]; 14 | 15 | echo "Id : $id" . PHP_EOL; 16 | echo "Name : $name" . PHP_EOL; 17 | echo "Email : $email" . PHP_EOL; 18 | } 19 | 20 | $connection = null; 21 | -------------------------------------------------------------------------------- /TestConnection.php: -------------------------------------------------------------------------------- 1 | getMessage() . PHP_EOL; 17 | } 18 | -------------------------------------------------------------------------------- /TestFetch.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 12 | $statement->bindParam("username", $username); 13 | $statement->bindParam("password", $password); 14 | $statement->execute(); 15 | 16 | if ($row = $statement->fetch()) { 17 | echo "Sukses Login : " . $row["username"] . PHP_EOL; 18 | } else { 19 | echo "Gagal Login" . PHP_EOL; 20 | } 21 | 22 | $connection = null; 23 | -------------------------------------------------------------------------------- /TestTransaction.php: -------------------------------------------------------------------------------- 1 | beginTransaction(); 8 | 9 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('eko@test.com', 'hi')"); 10 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('eko@test.com', 'hi')"); 11 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('eko@test.com', 'hi')"); 12 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('eko@test.com', 'hi')"); 13 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('eko@test.com', 'hi')"); 14 | 15 | $connection->commit(); 16 | 17 | $connection = null; 18 | -------------------------------------------------------------------------------- /TestPrepareIndexWithoutBind.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 12 | $statement->execute([$username, $password]); 13 | 14 | $success = false; 15 | $find_user = null; 16 | foreach ($statement as $row) { 17 | // sukses 18 | $success = true; 19 | $find_user = $row["username"]; 20 | } 21 | 22 | if ($success) { 23 | echo "Sukse login : " . $find_user . PHP_EOL; 24 | } else { 25 | echo "Gagal login" . PHP_EOL; 26 | } 27 | 28 | $connection = null; 29 | -------------------------------------------------------------------------------- /TestSqlInjection.php: -------------------------------------------------------------------------------- 1 | query($sql); 14 | 15 | $success = false; 16 | $find_user = null; 17 | foreach ($statement as $row) { 18 | // sukses 19 | $success = true; 20 | $find_user = $row["username"]; 21 | } 22 | 23 | if ($success) { 24 | echo "Sukse login : " . $find_user . PHP_EOL; 25 | } else { 26 | echo "Gagal login" . PHP_EOL; 27 | } 28 | 29 | $connection = null; 30 | -------------------------------------------------------------------------------- /TestTransactionRollback.php: -------------------------------------------------------------------------------- 1 | beginTransaction(); 8 | 9 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('budi@test.com', 'hi')"); 10 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('budi@test.com', 'hi')"); 11 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('budi@test.com', 'hi')"); 12 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('budi@test.com', 'hi')"); 13 | $connection->exec("INSERT INTO comments(email, comment) VALUES ('budi@test.com', 'hi')"); 14 | 15 | $connection->rollBack(); 16 | 17 | $connection = null; 18 | -------------------------------------------------------------------------------- /TestRepository.php: -------------------------------------------------------------------------------- 1 | insert($comment); 15 | // 16 | //var_dump($newComment->getId()); 17 | 18 | //$comment = $repository->findById(32); 19 | //var_dump($comment); 20 | 21 | $comments = $repository->findAll(); 22 | var_dump($comments); 23 | 24 | $connection = null; 25 | -------------------------------------------------------------------------------- /TestPrepareIndex.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 12 | $statement->bindParam(1, $username); 13 | $statement->bindParam(2, $password); 14 | $statement->execute(); 15 | 16 | $success = false; 17 | $find_user = null; 18 | foreach ($statement as $row) { 19 | // sukses 20 | $success = true; 21 | $find_user = $row["username"]; 22 | } 23 | 24 | if ($success) { 25 | echo "Sukse login : " . $find_user . PHP_EOL; 26 | } else { 27 | echo "Gagal login" . PHP_EOL; 28 | } 29 | 30 | $connection = null; 31 | -------------------------------------------------------------------------------- /TestPrepare.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 12 | $statement->bindParam("username", $username); 13 | $statement->bindParam("password", $password); 14 | $statement->execute(); 15 | 16 | $success = false; 17 | $find_user = null; 18 | foreach ($statement as $row) { 19 | // sukses 20 | $success = true; 21 | $find_user = $row["username"]; 22 | } 23 | 24 | if ($success) { 25 | echo "Sukse login : " . $find_user . PHP_EOL; 26 | } else { 27 | echo "Gagal login" . PHP_EOL; 28 | } 29 | 30 | $connection = null; 31 | -------------------------------------------------------------------------------- /Model/Comment.php: -------------------------------------------------------------------------------- 1 | id; 20 | } 21 | 22 | /** 23 | * @param int|null $id 24 | */ 25 | public function setId(?int $id): void 26 | { 27 | $this->id = $id; 28 | } 29 | 30 | /** 31 | * @return string|null 32 | */ 33 | public function getEmail(): ?string 34 | { 35 | return $this->email; 36 | } 37 | 38 | /** 39 | * @param string|null $email 40 | */ 41 | public function setEmail(?string $email): void 42 | { 43 | $this->email = $email; 44 | } 45 | 46 | /** 47 | * @return string|null 48 | */ 49 | public function getComment(): ?string 50 | { 51 | return $this->comment; 52 | } 53 | 54 | /** 55 | * @param string|null $comment 56 | */ 57 | public function setComment(?string $comment): void 58 | { 59 | $this->comment = $comment; 60 | } 61 | 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Repository/CommentRepository.php: -------------------------------------------------------------------------------- 1 | connection->prepare($sql); 29 | $statement->execute([$comment->getEmail(), $comment->getComment()]); 30 | 31 | $id = $this->connection->lastInsertId(); 32 | $comment->setId($id); 33 | 34 | return $comment; 35 | } 36 | 37 | public function findById(int $id): ?Comment 38 | { 39 | $sql = "SELECT * FROM comments WHERE id = ?"; 40 | $statement = $this->connection->prepare($sql); 41 | $statement->execute([$id]); 42 | 43 | if ($row = $statement->fetch()) { 44 | return new Comment( 45 | id: $row["id"], 46 | email: $row["email"], 47 | comment: $row["comment"] 48 | ); 49 | } else { 50 | return null; 51 | } 52 | } 53 | 54 | public function findAll(): array 55 | { 56 | $sql = "SELECT * FROM comments"; 57 | $statement = $this->connection->query($sql); 58 | 59 | $array = []; 60 | 61 | while ($row = $statement->fetch()) { 62 | $array[] = new Comment( 63 | id: $row["id"], 64 | email: $row["email"], 65 | comment: $row["comment"] 66 | ); 67 | } 68 | 69 | return $array; 70 | } 71 | 72 | } 73 | 74 | } 75 | --------------------------------------------------------------------------------