PDO connection / prepare and execute in a function
<p>I think i am getting a little head of myself with the below, what i want to do is have a one PDO and the prepare and execute called in a function.</p>

<p>I was trying to save having duplicate code with the PDO in every function.</p>

<p>If someone could point me in the right direction that would be greatfull and also any advise, i am currently trying to figure it all out from articles. then below gives me Call to a member function prepare() on a non-object in...</p>

<p><strong>2 additional questions</strong></p>

<ol>
<li>Should i put a try in the function for the execute and prepare, is
this common practice?</li>
<li>Is it possible to put the $db in a function
    and call it when needed so i ca put the die back in?</li>
</ol>

<p>any help much appreciated.</p>

<pre><code>ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);

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

function update($db, $fn, $ln, $email, $offers, $vlue, $responce)
{
    $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())");
    $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);
    $stmt-&gt;bindParam(':kkt_rsp', $vlue, PDO::PARAM_STR);
    $stmt-&gt;bindParam(':kkt_respState', $responce, PDO::PARAM_STR);
    $stmt-&gt;execute();
    $stmt = null;
}

$fn = 'test';
$ln = 'test';
$email = 'tesst@test,com';
$offers = '1';
$vlue = 'value';
$responce = 'resp';

update($db, $fn, $ln, $email, $offers, $vlue, $responce);
</code></pre>
