How can I make this request work in D7?

I am trying to rewrite this database query from line 52 of my site template.php D6

  $uid = db_query('SELECT pm.author FROM {pm_message} pm INNER JOIN {pm_index} pmi ON pmi.mid = pm.mid AND pmi.thread_id = %d WHERE pm.author <> %d ORDER BY pm.timestamp DESC LIMIT 1', $thread['thread_id'], $user->uid);

to D7 standards.

But he keeps giving me

Recoverable fatal error: argument 2 passed to db_query () must be an array, the given string is called by C: \ wamp2 \ www \ site-name \ sites \ all \ themes \ simpler \ template.php on line 52 and is defined in db_query () (line 2313 C:. \ Wamp2 \ site-name WWW \\ includes \ databases \ database.inc)

This database query is part of the template.php fragment that displays custom images in the private messaging module and makes it look like Facebook or another social networking site. Here you can see the full fragment. Since private messages have a unified value of $ participants (or message flow), this database query basically tries to isolate the last author, except for the current user.

What is the correct syntax?

+3
source share
1 answer

As the error message says: "Argument 2 passed to db_query () must be an array ...".

Drupal 7 switched the database layer to use PDO , so replacing the replacement element in db_query () has changed - try:

$query = 'SELECT pm.author FROM {pm_message} pm'
  . ' INNER JOIN {pm_index} pmi ON pmi.mid = pm.mid AND pmi.thread_id = :thread_id'
  . ' WHERE pm.author <> :uid'
  . ' ORDER BY pm.timestamp DESC LIMIT 1';
$args = array(
  ':thread_id' => $thread['thread_id'],
  ':uid' => $user->uid,
);
$uid = db_query($query, $args)->fetchField();

. , .

->fetchField() - , (, ). , DatabaseStatementInterface.

+6

All Articles