Search code snippets, questions, articles...

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]);
        
    }
    
}
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

SELECT

INSERT

OOP

PHP

Search Index Data (The code snippet can also be found with below search text)

SELECT
INSERT
OOP
PHP
database
Was this helpful?
0 Comments