Nurul
Nurul

Reputation: 59

PDOstatement::execute() expect parameter 1 to be array, string given in

Please help me to correct this code. error at line

$stmt->execute($params = [], $query);

and when i open the file with dreamweaver every "$params=[]" is error.

-Databasse.php-

<?php 

include_once('connection.php'); //my connection is here

class Database extends connection{


public function __construct(){

    parent::__construct();
}

public function getRow($params = [], $query){
    try {
        $stmt = $this->datab->prepare($query);
        $stmt->execute($params);
        return $stmt->fetch();  
    } catch (PDOException $e) {
        throw new Exception($e->getMessage());  
    }

}

public function getRows($query, $params = []){
    try {
        $stmt = $this->datab->prepare($query);
        $stmt->execute($params);
        return $stmt->fetchAll();   
    } catch (PDOException $e) {
        throw new Exception($e->getMessage());  
    }
}

this is half of the source code database.php

Upvotes: 5

Views: 2822

Answers (1)

ivanivan
ivanivan

Reputation: 2225

Need to see more of the code - what is your query string, what array w/ what values are you trying to feed into it?

Typically your query will have question marks ? or might have keywords with colons :mykeyword as place holders for values, and then your array holds the values that are "prepared" and executed.

$query="select name from users where name=? and passwordHash=?";
$res=$connection->prepare($query);
$res->execute(array(
    "myuser",
    "dd02c7c2232759874e1c205587017bed"),
    );

// OR

$query="select name from users where name=:name and passwordHash=:passwordHash";
$res=$connection->prepare($query);
$res->execute(array(
    ":name" => "myuser",
    ":passwordHash" => "dd02c7c2232759874e1c205587017bed"),
    );

This does the query preparation, escaping, etc. and the "myuser" value replaces the first question mark and the hash string replaces the second.

Upvotes: 2

Related Questions