Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: JonTheNiceGuy/PhpBetterPractices
base: master
...
head fork: JonTheNiceGuy/PhpBetterPractices
compare: MySQL-Versus-PDO
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
4 README.md
@@ -3,4 +3,6 @@ PhpBetterPractices
I'm writing a series of blog posts, and I want to ensure that the code behind them is easy to understand. As such, I plan to expand on each blog post with a branch on this repo.
-The blog posts are at: https://jon.sprig.gs/blog/post/category/computers/php-better-practices
+The blog posts are at: https://jon.sprig.gs/blog/post/category/computers/php-better-practices
+
+This branch relates to my post https://jon.sprig.gs/blog/post/177
View
3  sources/01_mysql_connect.php
@@ -0,0 +1,3 @@
+<?php
+mysql_connect("localhost", "root", "");
+mysql_use_db("mysite");
View
14 sources/02_using_mysql_connect.php
@@ -0,0 +1,14 @@
+<?php
+mysql_connect("localhost", "root", "");
+mysql_use_db("mysite");
+$sql = "select userid, realname, lastloggedin from users "
+ . "where username = '" . mysql_real_escape_string($_POST['username'])
+ . "' and password = '" . mysql_real_escape_string($_POST['password']) . "'";
+$query = mysql_query($sql);
+$data = false;
+if (mysql_num_rows($query) == 1) {
+ $data = mysql_fetch_array($query);
+ echo "Hello {$data['realname']}, your userid is {$data['userid']} and "
+ . "you last logged in at " . date("H:i:s", strtotime($data['lastloggedin']))
+ . " on " . date("d-M-Y" , strtotime($data['lastloggedin']));
+}
View
16 sources/03_introducing_pdo.php
@@ -0,0 +1,16 @@
+<?php
+try {
+ $db = new PDO("mysql:host=localhost;dbname=mysite", "root", "");
+ $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $sql = "select userid, realname, lastloggedin from users where username = ? and password = ?";
+ $query = $db->prepare($sql);
+ $query->execute(array($_POST['username'], $_POST['password']));
+ $data = $query->fetch();
+ if ($data != false) {
+ echo "Hello {$data['realname']}, your userid is {$data['userid']} and "
+ . "you last logged in at " . date("H:i:s", strtotime($data['lastloggedin']))
+ . " on " . date("d-M-Y" , strtotime($data['lastloggedin']));
+ }
+} catch (PDOException $e) {
+ error_log("User unable to login: " . $e->getMessage());
+}
View
41 sources/04_using_fetchObject.php
@@ -0,0 +1,41 @@
+<?php
+try {
+ $db = new PDO("mysql:host=localhost;dbname=mysite", "root", "");
+ $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $sql = "select userid, realname, lastloggedin, username, password "
+ . "from users where username = ? and password = ?";
+ $query = $db->prepare($sql);
+ $query->execute(array($_POST['username'], $_POST['password']));
+ $data = $query->fetchObject('user');
+ if ($data != false) {
+ echo $data;
+ }
+} catch (PDOException $e) {
+ error_log("User unable to login: " . $e->getMessage());
+}
+
+class user
+{
+ // Columns from the database
+ protected $userid = null;
+ protected $realname = null;
+ protected $lastloggedin = null;
+ protected $username = null;
+ protected $password = null;
+ // Processed Data
+ protected $transformed_lastloggedin = null;
+
+ public function __construct()
+ {
+ if ($this->lastloggedin != null) {
+ $this->transformed_lastloggedin = date("H:i:s", strtotime($this->lastloggedin))
+ . " on " . date("d-M-Y" , strtotime($this->lastloggedin));
+ }
+ }
+
+ public function toString()
+ {
+ echo "Hello {$this->realname}, your userid is {$this->userid} and "
+ . "you last logged in at {$this->transformed_lastloggedin}";
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.