Created
October 26, 2022 17:29
-
-
Save JeffreyWay/2371cd20b38888ef67241e228723deab to your computer and use it in GitHub Desktop.
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 MySQL database. | |
$dsn = "mysql:host=localhost;port=3306;dbname=myapp;user=root;charset=utf8mb4"; | |
// Tip: This should be wrapped in a try-catch. We'll learn how, soon. | |
$pdo = new PDO($dsn); | |
$statement = $pdo->prepare("select * from posts where id = 1"); | |
$statement->execute(); | |
$post = $statement->fetch(PDO::FETCH_ASSOC); | |
echo "<li>" . $post['title'] . "</li>"; |
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 | |
require 'functions.php'; | |
// Connect to the MySQL database. | |
$dsn = "mysql:host=localhost;port=3306;dbname=myapp;user=root;charset=utf8mb4"; | |
// Tip: This should be wrapped in a try-catch. We'll learn how, soon. | |
$pdo = new PDO($dsn); | |
$statement = $pdo->prepare("select * from posts"); | |
$statement->execute(); | |
$posts = $statement->fetchAll(PDO::FETCH_ASSOC); | |
foreach ($posts as $post) { | |
echo "<li>" . $post['title'] . "</li>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//connection with mysql
$dns = "mysql:host=localhost;dbname=myapp;user=root;charset=utf8mb4";
$pdo = new PDO($dns);
$statement = $pdo->prepare("select * from posts where id=1");
$statement->execute();
$post = $statement->fetch(PDO::FETCH_ASSOC);
echo "