Drupal 7 db_query
Solution 1
db_query works differently in Drupal7.
$result = db_query("select * from {files} where fid = %d", $val['_fid']);
while ($data = db_fetch_object($result)) {
$thefile = $data;
}
becomes
$results = db_query("select * from {files} where fid = :fid", array(':fid' => $val['_fid']));
foreach($results as $result) {
// Do your thing for each result.
}
Solution 2
Try changing
$result = db_query("select * from {files} where fid = %d", $val['_fid']);
while ($data = db_fetch_object($result)) {
$thefile = $data;
}
to
$query = db_select('files', 'f')
->fields('f')
->condition('fid', $val['_fid']);
$thefile = $query->execute()->fetchObject();
The Drupal 7 database API docs http://drupal.org/node/310069
Solution 3
thanks JurgenR
for the answer.
Just to add one more thing: In drupal 6, we use '%s' for strings. In drupal 7, it is same for all. For example:
$results = db_query("select * from {files}
where filename = :fname", array(':fname' => $filename));
If you want to search for records starting with a pattern then query will be:
$results = db_query("select * from {files}
where filename = :fname", array(':fname' => $filename.'%'));
Hope this is useful.
John Phelan
Updated on June 04, 2022Comments
-
John Phelan almost 2 years
The perfect little module for what I am looking to do was made for drupal 6 but to my dismay it doesn't work on drupal 7. I've have learned that drupal 7 has a new api for the database. I have tried to get it to work but I am admittedly out of my league here. I am hoping some one could give me a little guidance. Specifically with the db_query.
function webform_image_validation_webform_validation_validate($validator_name, $items, $components, $rule) { $errors = array(); if ($items) { switch ($validator_name) { case 'max_image_size': $dimensions = explode('x', $rule['data']); foreach ($items as $key => $val) { if (is_numeric($val['_fid'])) { $result = db_query("select * from {files} where fid = %d", $val['_fid']); while ($data = db_fetch_object($result)) { $thefile = $data; } $image_info = image_get_info($thefile->filepath); if (webform_image_validation_validate_image($image_info, $dimensions[0], $dimensions[1], FALSE) === FALSE) { $errors[$key] = t('Your image did not match the required width and/or height. (') . $dimensions[0] . t(' x ') . $dimensions[1] . t(')'); } } }
This is the error I receive.
Argument 2 passed to db_query() must be an array, string given, called in /home/designco/public_html/dev/sites/all/modules/webform_image_validation/ webform_image_validation.module on line 69 and defined in /home/designco/public_html/dev/includes/database/database.inc on line 2310
It appears I need to add an array but I get lost there. Any help would be appreciated. I'm just trying to find out if I'm the right track.
-
madth3 almost 12 years
-
-
John Phelan almost 12 yearsI went ahead and gave it a try but now get this error. I do appreciate the help.
DOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'designco_drupal.files' doesn't exist: SELECT f.* FROM {files} f WHERE (fid = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => 8 )
-
Clive almost 12 yearsThere's no
{files}
table in Drupal 7, it's been renamed as{file_managed}
-
Web Assistant almost 12 yearsAh, of course! I should have realised that. Just replace db_select('files', 'f') with db_select('file_managed', 'f')