Raj
Raj

Reputation: 1437

Invalid parameter number issue using pdo in mysql

I have recently started doing mysql queries using pdo but I am getting the following error

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

My code is,

$conquery = $this->db->prepare("SELECT questions.quid,answers.catid,answers.ansid  
            FROM questions,answers where  questions.qtype  = ? and answers.userid = ? and (questions.quid = answers.quid)");

$conquery->execute(array('questions.qtype' => 'concern','answers.userid' =>$_SESSION['user_id'])); 

Upvotes: 0

Views: 46

Answers (2)

artragis
artragis

Reputation: 3713

you misunderstood how parameters work. THere are two ways to use parameters. And those ways cannot be used simultaneously.

indexed parameters :

when you use indexed parameters, you use ? inside your query. THen to bind your parameters, you use a simple indexed array. In your cas you would write :

$conquery = $this->db->prepare("SELECT questions.quid,answers.catid,answers.ansid  
        FROM questions,answers where  questions.qtype  = ? and answers.userid = ? and (questions.quid = answers.quid)");

 $conquery->execute(array( 'concern',$_SESSION['user_id']))

named paramameters :

When you use named parameters, you prepend a : to the parameter name (wich is right side og the equal sign) and then use a named array, in your case it would be :

$conquery = $this->db->prepare("SELECT questions.quid,answers.catid,answers.ansid  
        FROM questions,answers where  questions.qtype  = :qtype and answers.userid = :userid and (questions.quid = answers.quid)");

 $conquery->execute(array('qtype' => 'concern','userid' =>$_SESSION['user_id']))

Upvotes: 1

caRameL
caRameL

Reputation: 633

Your parameters are not named in your prepared statement (you are using '?'), you should try this :

$conquery->execute(array('concern',$_SESSION['user_id'])); 

Upvotes: 1

Related Questions