你好,我有方法讓我們說,如果行存在檢查:查詢的內置函數添加更多參數?
/**
* Method rowExists
*
* Checks if row exists with given parameters.
*
* @param name The name of the value in a column.
* @param column The name of the given column.
* @param table The name of the given table.
**/
private function rowExists($name, $column, $table)
{
$this->user = $this->pdo->prepare("SELECT * FROM ".$table." WHERE ".$column." = :name");
$this->user->execute(array(":name" => $name));
if ($this->user->rowCount() > 0)
{
return true;
}
else
{
return false;
}
}
有了這個,我可以檢查是否行存在
用法:
if ($this->rowExistsAnd($this->get['user_id'], $generatedCode, 'user_id', 'generated_code', 'account_verifications') === true) {
現在我所要求的,這個方法只支持1個參數來檢查
如果我想檢查WHERE兩列怎麼辦?
例子:
當前查詢它:
SELECT * FROM table WHERE column1 = value1
我想:
SELECT * FROM table WHERE column1 = value1 AND column2 = value2
我想用1種方法這樣做,不創造另一個r方法添加參數。 我該怎麼做?
編輯:
private function rowDoesExist($params)
{
if (count($params) < 4)
{
$this->user = $this->pdo->prepare("SELECT * FROM ".$params[0]." WHERE ".$params[1]." = :name");
$execute = array(":name" => $params[2]);
}
else
{
$this->user = $this->pdo->prepare("SELECT * FROM ".$params[0]." WHERE ".$params[1]." = :name AND ".$params[2]." = :name2");
$execute = array(":name" => $params[3], ":name2" => $params[4]);
}
$this->user->execute($execute));
}
用法:
$this->rowDoesExist(array('users', 'user_name', $username);
你可以使用數組,而不是單一的字符串。一個包含列的名稱和另一個 - 值。 – Havelock