0

Hello I have method let's say that checks if row exists:

    /**
    * 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;
        }
    }   

With this I can check if row exists

Usage:

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

Now what I am asking for, this method only supports 1 parameter for checking

What if I want to check WHERE two columns?

Example:

Current query it does:

SELECT * FROM table WHERE column1 = value1

I want:

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

I want to do so with 1 method, without creating another method with adding parameters. How can I do this?

Edit:

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

Usage:

    $this->rowDoesExist(array('users', 'user_name', $username);
4

4 回答 4

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 *如果你想要的只是行数,它有点没用。

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

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

对我来说,在countRows.

于 2013-05-18T09:26:45.707 回答
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;
    }
}   
于 2013-05-18T09:30:11.140 回答
0

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

于 2013-05-18T09:16:02.830 回答
0

有3个信息需要考虑:

  • 列名(例如user_id, generated_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) {
于 2013-05-18T09:28:30.263 回答