0

I get "PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined" using the below db_query() call.

db_query("SELECT image.:field1_fid as fid, image.:field2_data as file_data, image.:field3_list AS list FROM {content_:field4} AS image WHERE vid = :vid AND nid = :nid", array(':field1' => array_values($field), ':field2' => array_values($field), ':field3' => array_values($field), ':field4' => array_values($node_type), ':vid' => array_values($vid), ':nid' => array_values($nid)));

I also tried like this.

db_query("SELECT image.:field1_fid as fid, image.:field2_data as file_data, image.:field3_list AS list FROM {content_:field4} AS image WHERE vid = :vid AND nid = :nid", array(':field1' => $field, ':field2' => $field, ':field3' => $field, ':field4' => $node_type, ':vid' => $vid, ':nid' => $nid));

Wny idea what's wrong in my query?

3 Answers 3

1

You cannot use parameters instead of field names. If the field name is contained in a variable, you should use something similar to the following one, or use dynamic queries, as suggested from Clive. (Dynamic queries make more sense in this case, though.)

db_query("SELECT " . db_escape_field($field1_fid) . " AS fid, /* … */ FROM {" . db_escape_table($field4) - "} WHERE /* … */");

$field1_fid, and $field4 contains strings, not arrays.

In your query there is also something else that is wrong: If you have an array of possible values for a database field, then you should use the IN operator, such as in the following example, not the = operator.

SELECT * FROM {node} WHERE nid IN :nids
1
  • Thanks kiamlaluno...solved my problem using your method...I will try Clive method also as you suggested.
    – user8012
    Jul 19, 2012 at 0:08
2

A parameter to a query is something that will go in the WHERE/HAVING clause, it can't be used to substitute text into the SELECT or FROM (or any other part) part of the query. As you've only got two proper parameters in the query (those after the WHERE clause), and you're passing several more as the arguments, you get an error informing you of the count mismatch.

You're also trying to pass in arrays as single string parameter replacements to the SELECT and FROM parts of the query, which will just never work (even if you could use parameter replacement in those part of the query). When cast to a string, the array will simply be the string Array, which is pretty much definitely not what you're looking for.

If you want to build those parts of your query dynamically you'll need to use db_select() instead. This code isn't a complete representation of your example but should get you going:

$query = db_select('content_' . $table_name, 'image');

$query->addExpression('image.' . $field_name);
$query->addExpression('image.' . $field2_name);
// etc...

$query->condition('vid', $vid);

$results = $query->execute();
0

In your example, $field is an array, when db_query() expect an array of strings, not an array of arrays.

Your Answer

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

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