我正在寻找的完美小模块是针对drupal 6制作的,但令我沮丧的是它不适用于drupal 7.我已经知道drupal 7有一个新的数据库api。我试图让它发挥作用,但我无可否认地离开了我的联盟。我希望有人可以给我一些指导。特别是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(')');
}
}
}
这是我收到的错误。
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
看起来我需要添加一个数组,但我迷失了。任何帮助,将不胜感激。我只是想弄清楚我是不是正确的赛道。
答案 0 :(得分:14)
db_query在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.
}
答案 1 :(得分:1)
尝试更改
$result = db_query("select * from {files} where fid = %d", $val['_fid']);
while ($data = db_fetch_object($result)) {
$thefile = $data;
}
到
$query = db_select('files', 'f')
->fields('f')
->condition('fid', $val['_fid']);
$thefile = $query->execute()->fetchObject();
Drupal 7数据库API文档http://drupal.org/node/310069
答案 2 :(得分:1)
感谢JurgenR
获得答案。
再添加一件事:在drupal 6中,我们对字符串使用'%s'。在drupal 7中,对所有人来说都是一样的。 例如:
$results = db_query("select * from {files}
where filename = :fname", array(':fname' => $filename));
如果您要搜索以模式开头的记录,则查询将为:
$results = db_query("select * from {files}
where filename = :fname", array(':fname' => $filename.'%'));
希望这很有用。