PDO connection / prep and execute in their own functions I'm currently changing some legacy code over to PDO.  The below code works, but not 100%.  I would like to know if it's best practice or if anything can be done better.

//Database Array
$config['db'] = array(
    'host' =&gt; 'localhost',
    'username' =&gt; 'root',
    'password' =&gt; 'root',
    'dbname' =&gt; 'local');

//New PDO
$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Check connection is ok
try {
    $db-&gt;exec("SET CHARACTER SET utf8");
}
catch (PDOException $ex) {
    print "Error!: " . $ex-&gt;getMessage() . "&lt;br/&gt;";
    die();
}

//Update users function
function update($db, $fn, $ln, $email, $offers, $vlue, $response)
{
    $stmt = $db-&gt;prepare("insert into local (fName_765, lName_765, email_765, signup_765) values (:fname, :lname, :email, :signup, NOW())");
    $stmt-&gt;bindParam(':fname', $fn, PDO::PARAM_STR);
    $stmt-&gt;bindParam(':lname', $ln, PDO::PARAM_STR);
    $stmt-&gt;bindParam(':email', $email, PDO::PARAM_STR);
    $stmt-&gt;bindParam(':signup', $offers, PDO::PARAM_STR);
    try {
        $stmt-&gt;execute();
        print $db-&gt;lastInsertId(); //show ID
        return true;
    }
    catch (PDOException $e) {
        print "Error!: " . $e-&gt;getMessage() . "&lt;br/&gt;"; // show error
        return false;
    }
}

//Test Attributes
$fn = 'test';
$ln = 'test';
$email = 'tesst@test,com';
$offers = '1';

if (!update($db, $fn, $ln, $email, $offers, $vlue, $response)) {
    echo "no update there is a slight problem";
} else {
    echo "it seemed to work";
}


Midified version:

$functionError = array();

//Database Array
$config['db'] = array(
    'host' =&gt; 'localhost',
    'username' =&gt; 'root',
    'password' =&gt; 'root',
    'dbname' =&gt; 'root');

//Check connection is ok
try {
    $db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
    $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
    $functionError[] = "&lt;p&gt;{DB} - Error!: " . $e-&gt;getMessage() . "&lt;/p&gt;";
    errorhandler ($functionError);
    die ();
}

//Error Handler
function errorhandler ($functionError) {
    print_r ($functionError);
    echo "Sorry we are experiencing issues we are aware of the problem";

}

//Update users function
function update($db, $fn, $ln, $email, $offers, $vlue, $response)
{
    $stmt = $db-&gt;prepare("insert into kkt (fName_765, lName_765, email_765, signup_765, kkt_resp_765, kkt_respSate_765, stamp_765) values (:fname, :lname, :email, :signup, :kkt_rsp, :kkt_respState, NOW())");

    $parameters = array(
        ':fname' =&gt; $fn,
        ':lname' =&gt; $ln,
        ':email' =&gt; $email,
        ':signup' =&gt; $offers,
        ':kkt_rsp' =&gt; $vlue,
        ':kkt_respState' =&gt; $response);

    try {
        $stmt-&gt;execute($parameters);
        return true;
    }
    catch (PDOException $ex) {
        $functionError[] = "&lt;p&gt;{INSERT} - Error!: " . $ex-&gt;getMessage() . "&lt;/p&gt;";
        errorhandler ($functionError);
        return false;
    }
} 

//Test Attributes
$fn = 'test';
$ln = 'test';
$email = 'tesst@test,com';
$offers = '1';
$vlue = 'value';
$response = 'resp';

if (update($db, $fn, $ln, $email, $offers, $vlue, $response)) {
    echo "it seemed to work";
    print $db-&gt;lastInsertId();
    header('Location: thank-you.php');
    exit;
}
Here are a few things I noticed.


If you are going to catch a possible exception when you connect to your MySQL DB you want to include the PDO instantiation in the try block.

try {  
    $db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db'}['dbname'], $config['db']['username'], $config['db']['password']);
    $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db-&gt;exec("SET CHARACTER SET utf8");
}  catch(PDOException $e) {  
    print "Error!: " . $ex-&gt;getMessage() . "&lt;br/&gt;";
    die(); 
}

The arguments $vlue and $response don't do anything in your update function. You probably want to get rid of those.
You can use a quicker syntax for binding parameters by defining an array that holds the values and passing it to PDOStatement::execute(). This has the benefit of less function calls. Doing it this way will default all parameter value datatypes to be PDO::PARAM_STR, which matches what you are doing currently.

$parameters = array(
    ':fname'  =&gt; $fn,
    ':lname'  =&gt; $ln,
    ':email'  =&gt; $email,
    ':signup' =&gt; $offers
);
$stmt-&gt;execute($parameters);

The point of exceptions is to be able to handle, on your own terms, exceptional events. If a PDO exception occurs in your update function, more than likely it won't/shouldn't know how to handle it. It makes more sense for the code that calls update to take care of that situation. So I would suggest moving the try/catch block to wherever the update function is called.

   try {
       update($db, $fn, $ln, $email, $offers, $vlue, $response)
   } catch (PDOException $e) {
       echo "no update there is a slight problem " . $e-&gt;getMessage();
   }