php
PHP OOP insert, select from DB
<?php
class Test extends Dbn {
//without prepared statements
public function getUsers() {
$sql = "SELECT * FROM users";
$stmt = $this->connect()->query($sql);
while($row = $stmt->fetch()) {
echo $row['firstname'];
}
}
//with prepared statements
public function getUsersStmt($firstname, $lastname) {
$sql = "SELECT * FROM users WHERE firstname = ? && lastname = ?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$firstname, $lastname]);
$names = $stmt->fetchAll();
foreach ($names AS $name) {
echo $name['firstname'] . " " . $name['lastname'];
}
}
//Insert prepared statements
public function inserUser($firstname, $lastname, $email) {
$sql = "INSERT INTO users (firstname, lastname, email) values (?,?,?)";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$firstname, $lastname, $email]);
}
}
SELECT
INSERT
OOP
PHP
Was this helpful?
Similar Posts