Monday, January 13, 2014

Simple PHP PDO Class Implementation

Simple PHP PDO Class Implementation

$host = "127.0.0.1";
$db = "sample";
$username = "root";
$password = "";

$database = new PDO("mysql:host=" . $host . ";dbname=" . $db, $username, $password);

function find_user($username) {
 global $database;
 $p = $database->prepare("SELECT * FROM member WHERE username = ?");
 $p->execute(array($username));
 return $p->fetchObject();
}

$results = find_user("admin");
var_dump($results);

Out put of this function will be like bellow.
object(stdClass)[3]
  public 'id' => string '4' (length=1)
  public 'username' => string 'admin' (length=5)
  public 'password' => string '240824aa0487bca3bdd4deb847954b76' (length=32)
  public 'email' => string 'bthssssiban@gmail.com' (length=17)
  public 'postcode' => string 'Q2Z23S' (length=6)
  public 'status' => string 'Inactive' (length=8)
  public 'code' => string '4172f3101212a2009c74b547b6ddf935' (length=32)
  public '_date' => string '2011-02-17' (length=10)


echo $results->username; will prints the output of the username of value from the database.

No comments:

Post a Comment