在where子句中使用数组作为参数时出错

时间:2019-06-25 15:45:04

标签: mysql arrays pdo query-string

我需要从查询字符串中获取一个数组,并从数据库中获取包含某些数组项的所有记录。我正在尝试以下内容:

$phone_numbers = array();
if(!empty($_GET['phone_numbers']))
    $phone_numbers = join(',', array_fill(0, count($_GET['phone_numbers']), '?'));

getUsersByPhoneNumbers($phone_numbers); //<<<< linha 30

public function getUsersByPhoneNumbers($phone_numbers) {
    $stmt = $this->pdo->prepare('SELECT * FROM  tbl_users 
                                 WHERE phone_number IN ('.$phone_numbers.')');

    $params = array();
    $params['phone_numbers'] = array_push($phone_numbers); //<<<< linha 856
    $stmt->execute($params);
    return $stmt;
}

但是,我遇到以下错误:

Warning: array_push() expects parameter 1 to be array, string given in /home/deskeep/www/controllers/ControllerUser.php on line 856

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number in /home/deskeep/www/rest/get_contacts.php:30 

Stack trace: #0 {main} thrown in /home/deskeep/www/rest/get_contacts.php on line 30

该如何解决?谢谢。

0 个答案:

没有答案