Skip to content

Instantly share code, notes, and snippets.

@SitesByYogi
Created June 21, 2023 22:32
Show Gist options
  • Save SitesByYogi/b30521042623b644394f35fa6e0a8630 to your computer and use it in GitHub Desktop.
Save SitesByYogi/b30521042623b644394f35fa6e0a8630 to your computer and use it in GitHub Desktop.
PHP database interaction
<?php
// Connect to the database
$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "root";
$password = "password";
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// Execute a query
$sql = "SELECT * FROM users";
$stmt = $db->query($sql);
// Fetch results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row["name"] . "<br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment