3
votes

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.

3

3 Answers

15
votes

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
1
votes

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

1
votes

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.