Created
June 21, 2023 22:32
-
-
Save SitesByYogi/b30521042623b644394f35fa6e0a8630 to your computer and use it in GitHub Desktop.
PHP database interaction
This file contains 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 | |
// 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