Is there any way I could improve this script, such as by writing it in fewer lines (more efficient)? I'm very interested in keeping my code dry and fast! Also, if there is a way to make it more dynamic don't hesitate to tell me!
<?php
/* Connect to an MYSQL database Using credentials and PDO(PHP DATA OBJECT) */
$dsn = 'mysql:host=**;dbname=**';
$user = '**';
$password = '**';
$connection = new PDO($dsn, $user, $password);
$table = $_POST['table'];
$querySQL = "";
$datum = $_POST['datum'];
//SELECT content,author FROM quote LIMIT 10
//INSERT INTO birthday (date,name) VALUES ('2012-05-05','henk')
switch ($table) {
case "quote":
$querySQL = "SELECT content,author FROM quote WHERE datum = ? LIMIT 10";
$stmt = $connection->prepare($querySQL);
$stmt->execute(array($datum));
break;
case "birthday":
$querySQL = "SELECT date,name FROM birthday LIMIT 10 WHERE datum = ? LIMIT 10";
$stmt = $connection->prepare($querySQL);
$stmt->execute(array($datum));
break;
case "facts":
$querySQL = "SELECT content from facts WHERE datum = ? AND category = ? LIMIT 10";
$categorie = $_POST['categorie'];
$stmt = $connection->prepare($querySQL);
$stmt->execute(array($datum, $categorie));
break;
case "lifehacks":
$querySQL = "SELECT content from lifehacks WHERE datum = ? LIMIT 10";
$stmt = $connection->prepare($querySQL);
$stmt->execute(array($datum));
break;
}
if(isset($stmt)) {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo $json;
}
?>