1

激活带有一些 PHP 验证的规则时,我收到一条 SQL 错误消息。该规则的目标是检查用户在过去 24 小时内是否投票超过 2 次。这是我使用的代码:

// Configure your settings: 
$daily_limit = 2;
$content_type = 'node';
$day = strtotime(date('Y-m-d'))-1;

// Load the active user account
global $user;

// Drupal has a security feature called the Database Abstraction Layer. 
// You have to build DB queries with variables that are defined in an array.
// Define the query string, where ":drupaluid" etc. are arbitrary placeholders

$query = 'SELECT * FROM votingapi_vote WHERE uid=:drupaluid AND content_type=:drupaltype AND timestamp > :day';

// Define each placeholder
$variables = array(':drupaluid' => $user->uid, ':drupaltype' => $content_type, ':day' => $day);

// Query the Drupal database
$result = db_query($query, $variables);

// Count the number of rows returned from the Drupal database
// From: http://api.drupal.org/api/drupal/includes%21database%21database.inc/function/db_query/7
$nodecount = $result->rowCount();

// Set the flag as to whether this Rule is effective or not
if ( $nodecount >= $daily_limit) {
    return TRUE; // You will be over the accepted quota
} else {
    return FALSE; // Still got room. Allow the new node.
}

我试图调试查询,但即使是一个简单的查询$query = 'SELECT * FROM votingapi_vote WHERE uid = 1也不能在 Php 中工作,而它在 mysql 控制台中工作。有人看到这个错误吗?

4

1 回答 1

0

DatabaseStatementInterface::rowCount特别返回

受最后执行的 DELETE、INSERT 或 UPDATE 语句影响的行数。

它不用于检索结果集的计数。为此,请使用计数查询,或者简单地使用 PHP 函数

$result = db_query($query, $variables)->fetchAll();

$nodecount = count($result);
于 2013-03-30T12:35:14.020 回答