Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 550
Nurul Avatar asked Oct 29 '22 15:10

Nurul


1 Answers

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.

like image 60
ivanivan Avatar answered Nov 15 '22 05:11

ivanivan