Created
August 19, 2025 22:56
-
-
Save uchoamaster/a7385fafa68f3cd59b38048a911a8e98 to your computer and use it in GitHub Desktop.
1. Usando mysqli (Estilo Procedural) Exemplo completo: Conexão + Queries (INSERT, SELECT, UPDATE, DELETE).
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 | |
$conexao = mysqli_connect($host, $usuario, $senha, $banco); | |
if (!$conexao) { | |
die("Erro: " . mysqli_connect_error()); | |
} | |
// INSERT com prepared statement | |
$stmt = mysqli_prepare($conexao, "INSERT INTO usuarios (nome, email) VALUES (?, ?)"); | |
mysqli_stmt_bind_param($stmt, "ss", $nome, $email); // "ss" para duas strings | |
$nome = "João Silva"; | |
$email = "[email protected]"; | |
mysqli_stmt_execute($stmt); | |
echo "Inserido ID: " . mysqli_insert_id($conexao) . "<br>"; | |
// SELECT | |
$stmt = mysqli_prepare($conexao, "SELECT * FROM usuarios WHERE nome = ?"); | |
mysqli_stmt_bind_param($stmt, "s", $nome); | |
$nome = "João Silva"; | |
mysqli_stmt_execute($stmt); | |
mysqli_stmt_bind_result($stmt, $id, $nome_res, $email_res); | |
while (mysqli_stmt_fetch($stmt)) { | |
echo "ID: $id, Nome: $nome_res, Email: $email_res<br>"; | |
} | |
// UPDATE | |
$stmt = mysqli_prepare($conexao, "UPDATE usuarios SET email = ? WHERE nome = ?"); | |
mysqli_stmt_bind_param($stmt, "ss", $novo_email, $nome); | |
$novo_email = "[email protected]"; | |
$nome = "João Silva"; | |
mysqli_stmt_execute($stmt); | |
echo "Atualizado: " . mysqli_stmt_affected_rows($stmt) . " registro(s)<br>"; | |
// DELETE | |
$stmt = mysqli_prepare($conexao, "DELETE FROM usuarios WHERE nome = ?"); | |
mysqli_stmt_bind_param($stmt, "s", $nome); | |
$nome = "João Silva"; | |
mysqli_stmt_execute($stmt); | |
echo "Deletado: " . mysqli_stmt_affected_rows($stmt) . " registro(s)<br>"; | |
// Fecha statement e conexão | |
mysqli_stmt_close($stmt); | |
mysqli_close($conexao); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment