Drupal database prefix not used

spamsammy - August 12, 2009 - 17:53
Project:Activity Stream
Version:6.x-2.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

Watchdog reports:

Table 'xxxxx.activitystream_accounts' doesn't exist query:

SELECT a.uid, a.userid, a.password, a.feed, a.module FROM activitystream_accounts a LEFT JOIN users u on a.uid = u.uid WHERE a.lastfetch < date_sub(now( ) , INTERVAL 1 HOUR) AND u.access > unix_timestamp(date_sub(now( ) , INTERVAL 30 day)) ORDER BY a.lastfetch DESC, u.access DESC in /xxxx/httpdocs/sites/all/modules/activitystream/activitystream.module on line 473.

Investigating the function activitystream_cron:
<?php
function activitystream_cron() {
 
module_load_include('inc', 'node', 'node.pages');
 
$number_of_hours = variable_get('activitystream_cron_hours', 1);
 
$active_days = variable_get('activitystream_active_days', 30);
 
$result = db_query('SELECT a.uid, a.userid, a.password, a.feed, a.module FROM activitystream_accounts a LEFT JOIN users u on a.uid = u.uid  WHERE a.lastfetch < date_sub(now( ) , INTERVAL %d HOUR) AND u.access > unix_timestamp(date_sub(now( ) , INTERVAL %d day)) ORDER BY a.lastfetch DESC, u.access DESC', $number_of_hours, $active_days);
 
$users = array();
  while (
$user = db_fetch_object($result)) {
   
$users[] = $user;
  }
 
$num_users = activitystream_update_streams($users);
  if (
$num_users) {
   
// Add an informative message in the watchdog
   
watchdog('activitystream', 'Processed %count users.', array('%count' => $num_users));
  } 
}
?>

The offending line 473: My implementation uses a drupal table prefix that seems abscent:
<?php
  $result
= db_query('SELECT a.uid, a.userid, a.password, a.feed, a.module FROM activitystream_accounts a LEFT JOIN users u on a.uid = u.uid  WHERE a.lastfetch < date_sub(now( ) , INTERVAL %d HOUR) AND u.access > unix_timestamp(date_sub(now( ) , INTERVAL %d day)) ORDER BY a.lastfetch DESC, u.access DESC', $number_of_hours, $active_days);
?>

Possible fix? I'm not familiar with the core enough to know if raw table names are used and the prefix is added somewhere down the line.
<?php
  $result
= db_query("SELECT a.uid, a.userid, a.password, a.feed, a.module FROM ".$db_prefix."activitystream_accounts a LEFT JOIN ".$db_prefix."users u on a.uid = u.uid  WHERE a.lastfetch < date_sub(now( ) , INTERVAL %d HOUR) AND u.access > unix_timestamp(date_sub(now( ) , INTERVAL %d day)) ORDER BY a.lastfetch DESC, u.access DESC", $number_of_hours, $active_days);
?>

#1

akalsey - August 12, 2009 - 19:28
Title:function activitystream_cron() fails - missing $db_prefix ? line 473 when using a drupal database prefix» Drupal database prefix not used

Drupal adds the prefixes automatically if the table names are wrapped in curly brackets.

Changing to this will fix it...

<?php
$result
= db_query('SELECT a.uid, a.userid, a.password, a.feed, a.module FROM {activitystream_accounts} a LEFT JOIN {users} u on a.uid = u.uid  WHERE a.lastfetch < date_sub(now( ) , INTERVAL %d HOUR) AND u.access > unix_timestamp(date_sub(now( ) , INTERVAL %d day)) ORDER BY a.lastfetch DESC, u.access DESC', $number_of_hours, $active_days);
?>

#2

spamsammy - August 13, 2009 - 11:47

Ahhh a much better fix! Thanks for the lesson :)

#3

akalsey - November 7, 2009 - 05:48
Status:active» fixed

Fix commited in rev 1.2.2.15

#4

System Message - November 21, 2009 - 05:50
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.