1

I got an SQL error message when activating a rule with some Php validation. The goal of that rule is to check if a user has voted more than 2 times in the last 24 hours. Here is the code I used:

// 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.
}

I tried to debug the query but even a simple query like $query = 'SELECT * FROM votingapi_vote WHERE uid = 1 is not working in Php whereas It work in mysql console. Does anyone see this error?

apaderno
  • 28,547
  • 16
  • 75
  • 90

1 Answers1

0

DatabaseStatementInterface::rowCount specifically returns

The number of rows affected by the last DELETE, INSERT, or UPDATE statement executed.

It's not used to retrieve the count of a result set. To do that use a count query, or simply use PHP functions

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

$nodecount = count($result);
Clive
  • 36,918
  • 8
  • 87
  • 113