php创建更新函数、绑定、数组和PDO

php创建更新函数、绑定、数组和PDO,php,arrays,binding,pdo,sql-update,Php,Arrays,Binding,Pdo,Sql Update,好的,我将继续我的学习之旅,以缓慢的速度适应PDO和OOP 这是我的问题。我正在尝试创建一个函数来处理mysql的更新,这让我觉得太复杂了,我也可以手动输入。我会处理很多表单的大更新,所以我想让这个函数可以重用,但我想我已经把它复杂化了,有没有一种更简洁的方法同时让代码更容易检查 这是我的更新功能: // take data from arrays, loop through and print each out // concatenate this onto SET and concat

好的,我将继续我的学习之旅,以缓慢的速度适应PDO和OOP

这是我的问题。我正在尝试创建一个函数来处理mysql的更新,这让我觉得太复杂了,我也可以手动输入。我会处理很多表单的大更新,所以我想让这个函数可以重用,但我想我已经把它复杂化了,有没有一种更简洁的方法同时让代码更容易检查

这是我的更新功能:

 // take data from arrays, loop through and print each out
 // concatenate this onto SET and concatenate the where clause
 // on the end unless there is no criteria in which case print nothing
 public function update_sql($table="users",$update_array,$criteria=""){

 $sql = 'UPDATE `'.$table.'` SET ';
 $sqlFieldParams = array();       
 // creating an array with `user_id` = :user_id etc etc
 foreach ($update_array as $fieldName => $fieldValue) {
     $sqlFieldParams [] = $fieldName . '= :' . $fieldName;
 }

 // concatenate but don't print where if there is no criteria passed
 $sql .= implode(', ', $sqlFieldParams) . ($criteria ? ' WHERE ' . $criteria : "");
 $this->query("$sql");
 }
我的绑定和执行函数,我还将其用于insert和其他需要绑定的语句

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

 // execute the update
 $this->execute();
 }
还有几个可重用的函数,这些函数在这个脚本中仅供参考

    // prepare our SQL Queries
public function query($query){
    $this->stmt = $this->dbh->prepare($query);
}

// use switch to select the appropriate type for the 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 the binding process
$this->stmt->bindValue($param, $value, $type);
}

// execute the prepared statement
public function execute(){
    return $this->stmt->execute();
}
现在是我可怕的更新声明

 $this->user_id = $_GET['id'];
 $this->user_activation_hash = $_GET['verification_code'];

 // create an array to pass these values to be set to the update function
 $update_array = array(
 'user_active'               => '1',
 'user_activation_hash'      => 'NULL',
 );

 // create the where clause
 $criteria = 'user_id = :user_id AND user_activation_hash = :user_activation_hash';

 // create the update statement
 // pass in values table, array & criteria
 $database->update_sql('users',$update_array,$criteria);

 // these are other values that need binding from the where clause
 $criteria_array = array(
    'user_id'                => "'.$this->user_id.'"
 );

 // join the set values of the update with the where values
 // in the one array to merge then in a for loop next
 $bind_array = array_merge($update_array, $criteria_array);

 $database->bind_execute($bind_array);

想法、反馈?更好的方法?如果你把它拆开,我想它只有5行,但我想我可能太复杂了?

我有以下简单的PDO插入或更新功能。没有什么花哨的,只有基本的:

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 as $Key => $Val)
            $Statement->bindValue($Key+1, $Val);
        $Statement->execute();
        $PDOConnection = null;
        return true;
    } 
    catch(PDOException $e) 
    {
        // you doe do something with error message like 
        // die('ERROR: ' . $e->getMessage());
        return false;
    }

}   
我将调用函数,如下例所示:

$Query = "insert into users set firstname=?, lastname=?, username=?, password=?";
$Paramaters = array($firstname, $lastname, $username, $password);

if (PDOInsertUpdate($Query, $Parameters))
{
  //after success
}
else
{
  //something went wrong!
}

为每个查询执行创建一个新的连接不是goI的方法,到目前为止,我还没有看到每个脚本都会多次执行这个函数,因为我可以只调用一个稍微修改过的函数,只在执行结束时调用多个查询,很少有例外…重复的