Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal query builder return all fields

How do I write a query using Drupal 7's query builder to return all the fields (SELECT *), not just the ones I specify through ->fields.

Edit: I tried something like

$query = db_select('table') ->condition('id', 2);

but when i echo it it's something like:

SELECT FROM {table} table WHERE (id = :db_condition_placeholder_0)

I haven't tested the query but my thoughts are the it will not work cause there is no * after SELECT.

like image 286
Revan Darth Avatar asked Oct 30 '13 13:10

Revan Darth


1 Answers

This is how you do it:

<?php

$myId = 5;
$result = db_select('table', 't')
    ->fields('t')
    ->condition('id', $myId, '=')
    ->execute()
    ->fetchAssoc();

?>

the above is equivelent to:

SELECT t.* FROM table as t WHERE t.id = 7

More info is on the API documentation found here: https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7

like image 65
Latheesan Avatar answered Nov 19 '22 03:11

Latheesan