Last active
April 24, 2016 18:33
-
-
Save alysdal/d5ffd1a120dda49e100550e404ccd0c1 to your computer and use it in GitHub Desktop.
Filer til web workshop 2016
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 | |
// database credentials | |
$dbHost = 'localhost'; | |
$dbName = 'webworkshop'; | |
$dbUser = 'root'; | |
$dbPass = 'root'; | |
try { | |
// attempt to connect to the database | |
$db = new PDO('mysql:host=' . $dbHost . ';dbname=' . $dbName . ';charset=UTF8', $dbUser, $dbPass); | |
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // fetch results to an associative array | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); // show warnings, useful for debugging | |
} catch(Exception $e) { | |
// if we get any errors, print them. | |
echo $e->getMessage(); | |
exit; | |
} | |
?> |
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_once("dbconnection.php"); | |
$name = 'Noam Chomsky'; | |
$lastlogin = date("Y-m-d g:i:s", time()); // "now" formatted date like 2016-04-20 12:00:00 | |
// this sql statement contains a placeholder for the variables $name (:name) and $lastlogin | |
$sql = "INSERT INTO persons (name, lastlogin) VALUES (:name, :lastlogin)"; | |
$statement = $db->prepare($sql); | |
// bind values to the SQL statement | |
$statement->bindValue(':name', $name); | |
$statement->bindValue(':lastlogin', $lastlogin); | |
// execute the sql statement | |
$statement->execute(); | |
?> |
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_once("dbconnection.php"); | |
$sql = "SELECT * FROM persons"; | |
$statement = $db->prepare($sql); | |
// execute the sql statement | |
$statement->execute(); | |
// get the result in array format. | |
$results = $statement->fetchAll(); | |
?> | |
<h2>Login historik</h2> | |
<ul> | |
<?php | |
foreach ($results as $row) { | |
echo "<li>"; | |
echo $row['lastlogin']; | |
echo " -- "; | |
echo $row['name']; | |
echo "</li>"; | |
} | |
?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment