Skip to content

Instantly share code, notes, and snippets.

@TorbenKoehn
Last active January 20, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TorbenKoehn/1c5e193f9f4a9cd4be34149ef3ff2933 to your computer and use it in GitHub Desktop.
Save TorbenKoehn/1c5e193f9f4a9cd4be34149ef3ff2933 to your computer and use it in GitHub Desktop.
<?php
class Todo
{
private $description;
private $completed;
private $due;
public function __construct($description = '', $completed = 0, $due = 'tomorrow')
{
$this->description = $description;
$this->completed = $completed;
$this->due = $due;
}
public function setDescription($description)
{
$this->description = $description;
}
public function setCompleted($completed)
{
$this->completed = $completed;
}
public function setDue($due)
{
$this->due = $due;
}
}
$host = "127.0.0.1";
$port = "3306";
$username = "root";
$password = "";
//By default, wrap a PDO inside a try-catch statement!
try {
$pdo = new PDO("mysql:host={$host};port={$port};dbname=test", $username, $password);
echo "Connected successfully";
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
//Prepare statement (not executed yet)
$statement = $pdo->prepare("SELECT * FROM todos");
//Execute it
$statement->execute();
echo "\n\PDO:\n\n";
//Store all data inside an object
//Works completely fine on private props!
$todos = $statement->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, Todo::class);
var_dump($todos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment