2

what is the right sql query for drupal 7 if the query like this

SELECT MAX(nopeserta) 
FROM `1_jp_peserta` 
WHERE jpke=$q1 AND lomba=$q2 AND kelamin=$q3

i have tried in drupal like this

$q4 = db_query("SELECT MAX(nopeserta) AS nopeserta FROM {1_jp_peserta} 
WHERE jpke=:q1 AND lomba=:q2 AND kelamin=:q3",
array(':q1'=>'$q1',':q2'=>'$q2',':q3'=>'$q3'))->execute()->fetchAssoc();

but the result is

PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters 
were bound in jpeserta_form_submit() 
5

When you use db_query() the query is executed for you, I think the error you're getting is because you're re-running execute() on it. Also it looks like you're trying to interpolate variables into a string using single quotes, when you need to use double quotes (or none at all in your case).

Try this:

$q4 = db_query("
  SELECT MAX(nopeserta) AS nopeserta FROM {1_jp_peserta} WHERE jpke=:q1 AND lomba=:q2 AND kelamin=:q3", array(':q1'=>$q1,':q2'=>$q2,':q3'=>$q3))->fetchAssoc();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.