用于查询的内置函数添加更多参数?

时间:2013-05-18 09:04:11

标签: php oop

你好,我有方法可以说检查行是否存在:

    /**
    * 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个参数进行检查

如果我想查看两列的哪一列怎么办?

示例:

当前查询:

SELECT * FROM table WHERE column1 = value1

我想:

SELECT * FROM table WHERE column1 = value1 AND column2 = value2

我希望使用1方法完成此操作,而无需创建添加参数的其他方法。 我该怎么做?

编辑:

    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);

4 个答案:

答案 0 :(得分:2)

我建议像这样重写你的功能(未经测试):

private function countRows($table, array $criteria = null)
{
    $query = "SELECT COUNT(*) AS c FROM $table";
    if ($criteria) {
        $query .= ' WHERE ' . implode(' AND ', array_map(function($column) {
            return "$column = ?";
        }, array_keys($criteria));
    }
    $stmt = $this->pdo->prepare($query)
        or die('Failed to prepare query ' . $query);
    $stmt->execute(array_values($criteria));
    return $stmt->fetchColumn();
}

首先,如果你想要的只是行数,那对SELECT *来说就没用了。

其次,将过滤器标准(对于WHERE子句)作为关联数组更有意义:键将对应于列名称和值,以及它们的预期值。如果您想制作更具体的功能,只能使用一个标准,请继续:

private function countRowsBySingleCriteria($table, $column, $value)
{
  return $this->countRows($table, array($column => $value));
}

对我来说,更有意义的是在countRows中检查参数的类型。

答案 1 :(得分:0)

您可以使用数组作为附加参数,或使用func_num_args()func_get_arg()func_get_args()函数来获取函数额外参数

答案 2 :(得分:0)

需要考虑3种信息:

  • 列名称(例如user_idgenerated_code
  • 运营商(例如=
  • 要与之比较的值(例如$this->get['user_id']$generatedCode

我认为您应该使用数组来实现这一目标。

private function rowExists($table, array $wheres = array())
{
    $query = "SELECT COUNT(*) FROM {$table} WHERE 1 = 1";
    $params = array();

    foreach ($wheres as $where)
    {
      $query .= " AND {$where[0]} {$where[1]} ? "
      $params[] = $where[2];
    }

    $this->user = $this->pdo->prepare($query);
    $this->user->execute($params);

    if ($this->user->fetchcolumn() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

用法示例:

if ($this->rowExistsAnd('account_verifications', array(
   array('user_id', '=', $this->get['user_id']),
   array('generated_code', '=', $generatedCode)
)) === true) {

答案 3 :(得分:0)

试试这个......

 private function rowExists($name1,$name1, $column1,$column2 $table)
  {
    $cond=" where 1=1";
    if($column1)  
    $cond.=" and ". $column1."=:name1";
     if($column2) 
     $cond.=" and ". $column2."=:name2"
   $query = "SELECT * FROM ".$table.$cond;
    $this->user = $this->pdo->prepare($query);
    $this->user->execute(array(":name1" => $name1,":name2" => $name2));

    if ($this->user->rowCount() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}