Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I'm sorry if the question was already asked, I am new at this.

I have this error :
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
now that I have put the code in OOP, before it all worked well. Can somebody tell what I am missing ?

Thank you !

class DbModel
{
    private $dbConnect;

    function __construct()
    {
      $this->dbConnect = new DbConnect();
    }


    public function executeQuery($sql, $data)
    {
        $conn = $this->dbConnect->dbConnect();
        $stmt = $conn->prepare($sql);
        $values = array_values($data);
        $types = str_repeat('s', count($values));
        var_dump($values);
        $stmt->bindParam($types, ...$values);
        $stmt->execute();
        return $stmt;
        var_dump($stmt);
    }

    public function selectOne($table, $conditions)
    {
        $conn = $this->dbConnect->dbConnect();
        $sql = "SELECT * FROM $table";
        
        $i = 0;
        foreach ($conditions as $key => $value) {
            if ($i === 0) {
                $sql = $sql . " WHERE $key=?";
            } else {
                $sql = $sql . " AND $key=?";
            }
            $i++;
        }

        $sql = $sql . " LIMIT 1";
        $stmt = $this->executeQuery($sql, $conditions);
        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $records;

        // $stmt = $conn->prepare($sql);
        // $stmt->execute(22);
        // $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
        // var_dump($records);
        // var_dump($records);
        // return $records;
    }
}


What I have tried:

I search in other forums, I read this but can't understand how to apply it here :
is because the number of elements in $values & $keys is not the same or $keys contains more than 1 element.
Posted
Comments
CHill60 15-Sep-21 9:16am    
Dump the contents of $sql and $conditions. You have more (or less) "?" in your query than the data you are adding as parameters. It may be that one of the values in $conditions contains a comma?
Member 11746136 15-Sep-21 9:22am    
Console log your final sql statement you will find what you are sending to database and what is the error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900