php creating an update function, binding, arrays and PDO -


ok im continuing on journey of learning adapt pdo , oop , @ slow rate.

here issue. im trying create function handle updates mysql, feels complicated point type out manually. doing lot of handling big updates forms wanted make function reusable think way on complicated it, there more concise way while keeping code easy review?

this update function:

 // take data arrays, loop through , print each out  // concatenate onto set , concatenate clause  // on end unless there no criteria in case print nothing  public function update_sql($table="users",$update_array,$criteria=""){   $sql = 'update `'.$table.'` set ';  $sqlfieldparams = array();         // creating array `user_id` = :user_id etc etc  foreach ($update_array $fieldname => $fieldvalue) {      $sqlfieldparams [] = $fieldname . '= :' . $fieldname;  }   // concatenate don't print if there no criteria passed  $sql .= implode(', ', $sqlfieldparams) . ($criteria ? ' ' . $criteria : "");  $this->query("$sql");  } 

my function bind , execute use insert , other statements need binding.

 public function bind_execute($bind_array){  // bind values  foreach ($bind_array $field => $item) {      $this->bind(':'.$field,$item);  }   // execute update  $this->execute();  } 

and couple more reusable functions used in script reference

    // prepare our sql queries public function query($query){     $this->stmt = $this->dbh->prepare($query); }  // use switch select appropriate type value been passed // $param = placeholder name e.g username, $value = myusername public function bind($param, $value, $type = null){     if (is_null($type)) {         switch (true) {             case is_int($value):                 $type = pdo::param_int;                 break;             case is_bool($value):                 $type = pdo::param_bool;                 break;             case is_null($value):                 $type = pdo::param_null;                 break;             default:                 $type = pdo::param_str;         }     } // run binding process $this->stmt->bindvalue($param, $value, $type); }  // execute prepared statement public function execute(){     return $this->stmt->execute(); } 

and monstrous update statement

 $this->user_id = $_get['id'];  $this->user_activation_hash = $_get['verification_code'];   // create array pass these values set update function  $update_array = array(  'user_active'               => '1',  'user_activation_hash'      => 'null',  );   // create clause  $criteria = 'user_id = :user_id , user_activation_hash = :user_activation_hash';   // create update statement  // pass in values table, array & criteria  $database->update_sql('users',$update_array,$criteria);   // these other values need binding clause  $criteria_array = array(     'user_id'                => "'.$this->user_id.'"  );   // join set values of update values  // in 1 array merge in loop next  $bind_array = array_merge($update_array, $criteria_array);   $database->bind_execute($bind_array); 

thoughts, feedback? better approach? guess 5 lines if strip down think might have over-complicated it?

i have following function simple pdo insert or update. nothing fancy basic:

function pdoinsertupdate($query, $parameters) {     try      {         $pdoconnection = new pdo('mysql:host='.db_host.';dbname='.db_name.'', db_user, db_pass);         $pdoconnection->setattribute(pdo::attr_errmode, pdo::errmode_exception);         $statement = $pdoconnection->prepare($query);         foreach ($parameters $key => $val)             $statement->bindvalue($key+1, $val);         $statement->execute();         $pdoconnection = null;         return true;     }      catch(pdoexception $e)      {         // doe error message          // die('error: ' . $e->getmessage());         return false;     }  }    

and call function in following example:

$query = "insert users set firstname=?, lastname=?, username=?, password=?"; $paramaters = array($firstname, $lastname, $username, $password);  if (pdoinsertupdate($query, $parameters)) {   //after success } else {   //something went wrong! } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -