habaneroio

Basic PDO Connection & Insert Example

Feb 18th, 2015
220
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     $options    =    array(
  4.                      PDO::ATTR_EMULATE_PREPARES    =>    false,
  5.                      PDO::ATTR_ERRMODE             =>    PDO::ERRMODE_EXCEPTION
  6.                      );
  7.  
  8.     $db         =    new PDO( 'mysql:host=HOSTNAME;dbname=DBNAME;charset=utf8', 'DBUSERNAME', 'DBPASSWORD', $options );
  9.  
  10.     $username   =    $_POST['username'];
  11.     $password   =    $_POST['password'];
  12.     $emailaddr  =    $_POST['emailaddr'];
  13.  
  14.     $stmt = $db->prepare("
  15.         INSERT INTO table( field1, field2, field3, field4, field5 )
  16.         VALUES( :field1, :field2, :field3, :field4, :field5 )
  17.         ");
  18.  
  19.     $stmt->execute( array(
  20.         ':field1' => $field1, ':field2' => $field2, ':field3' => $field3, ':field4' => $field4, ':field5' => $field5
  21.         ) );
  22.  
  23.     $affected_rows = $stmt->rowCount();
RAW Paste Data