0

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'])); 

2 Answers 2

1

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']))
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

Thank you so much for your help.I got a nice comparison of 2 methods described by artragis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.