[英]php creating an update function, binding, arrays and PDO
好的,我繼續以緩慢的速度學習適應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");
}
我的綁定和執行函數,我也用於插入和其他需要綁定的語句。
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!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.