Created
August 19, 2025 22:57
-
-
Save uchoamaster/a228a1e6fe5c35c45b8c3aee1c37e49d to your computer and use it in GitHub Desktop.
2. Usando mysqli (Estilo Orientado a Objetos) Mesmas queries, mas com OO.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Parâmetros de conexão | |
| $host = 'localhost'; | |
| $usuario = 'root'; | |
| $senha = ''; | |
| $banco = 'exemplo_db'; | |
| // Conexão | |
| $mysqli = new mysqli($host, $usuario, $senha, $banco); | |
| if ($mysqli->connect_error) { | |
| die("Erro: " . $mysqli->connect_error); | |
| } | |
| // INSERT | |
| $stmt = $mysqli->prepare("INSERT INTO usuarios (nome, email) VALUES (?, ?)"); | |
| $stmt->bind_param("ss", $nome, $email); | |
| $nome = "Maria Oliveira"; | |
| $email = "[email protected]"; | |
| $stmt->execute(); | |
| echo "Inserido ID: " . $mysqli->insert_id . "<br>"; | |
| // SELECT | |
| $stmt = $mysqli->prepare("SELECT * FROM usuarios WHERE nome = ?"); | |
| $stmt->bind_param("s", $nome); | |
| $nome = "Maria Oliveira"; | |
| $stmt->execute(); | |
| $stmt->bind_result($id, $nome_res, $email_res); | |
| while ($stmt->fetch()) { | |
| echo "ID: $id, Nome: $nome_res, Email: $email_res<br>"; | |
| } | |
| // UPDATE | |
| $stmt = $mysqli->prepare("UPDATE usuarios SET email = ? WHERE nome = ?"); | |
| $stmt->bind_param("ss", $novo_email, $nome); | |
| $novo_email = "[email protected]"; | |
| $nome = "Maria Oliveira"; | |
| $stmt->execute(); | |
| echo "Atualizado: " . $stmt->affected_rows . " registro(s)<br>"; | |
| // DELETE | |
| $stmt = $mysqli->prepare("DELETE FROM usuarios WHERE nome = ?"); | |
| $stmt->bind_param("s", $nome); | |
| $nome = "Maria Oliveira"; | |
| $stmt->execute(); | |
| echo "Deletado: " . $stmt->affected_rows . " registro(s)<br>"; | |
| // Fecha | |
| $stmt->close(); | |
| $mysqli->close(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment