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.
I was trying to save having duplicate code with the PDO in every function.
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...
2 additional questions
- Should i put a try in the function for the execute and prepare, is this common practice?
- Is it possible to put the $db in a function and call it when needed so i ca put the die back in?
any help much appreciated.
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'bhill');
$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'],
$config['db']['username'], $config['db']['password']);
try {
$db->exec("SET CHARACTER SET utf8");
//$db = null;
}
catch (PDOException $ex) {
print "Error!: " . $ex->getMessage() . "<br/>";
die();
}
function update($db, $fn, $ln, $email, $offers, $vlue, $responce)
{
$stmt = $db->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->bindParam(':fname', $fn, PDO::PARAM_STR);
$stmt->bindParam(':lname', $ln, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':signup', $offers, PDO::PARAM_STR);
$stmt->bindParam(':kkt_rsp', $vlue, PDO::PARAM_STR);
$stmt->bindParam(':kkt_respState', $responce, PDO::PARAM_STR);
$stmt->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);