Created
August 27, 2017 17:13
-
-
Save corysimmons/94e3a24d76a230aeb1702b620f2e896b to your computer and use it in GitHub Desktop.
Simple PHP CRUD CLI. Requires db with table `misc` and column `some_string`.
This file contains hidden or 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 | |
// Initialize database connection | |
$db = new PDO('mysql:host=localhost;dbname=phpcrud;charset=utf8mb4', 'root', ''); | |
switch ($argv[1]) { | |
// CREATE | |
case 'create': | |
$stmt = $db->prepare("INSERT misc SET some_string=?"); | |
$stmt->execute([$argv[2]]); | |
break; | |
// READ | |
case 'read': | |
$stmt = $db->query("SELECT * FROM misc"); | |
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
foreach ($results as $result) { | |
echo $result['some_string'] . "\n"; | |
} | |
break; | |
// UPDATE | |
case 'update': | |
$stmt = $db->prepare("UPDATE misc SET some_string=? WHERE some_string=?"); | |
$stmt->execute([$argv[3], $argv[2]]); | |
break; | |
// DELETE | |
case 'delete': | |
$stmt = $db->prepare("DELETE FROM misc WHERE some_string=?"); | |
$stmt->execute([$argv[2]]); | |
break; | |
// Instructions | |
default: | |
echo 'Usage: php -f crud.php create pie'; | |
echo 'Usage: php -f crud.php read'; | |
echo 'Usage: php -f crud.php update pie cake'; | |
echo 'Usage: php -f crud.php delete cake'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment