Drupal query builder return all fields
Asked Answered
R

1

7

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.

Rhapsodize answered 30/10, 2013 at 13:20 Comment(5)
Googling would have helped. api.drupal.org/api/drupal/includes!database!database.inc/…Galitea
I already searched (your link included), and I haven't found how to accomplish such a feat.Rhapsodize
Can you post what you've tried so far?Galitea
short answer: $id = 1; $results = db_query('SELECT * FROM table WHERE someId = :myId', array(':myId' => $id));Galitea
Edited what i tried out. I don't like your solution because I don't know how many fields I will have in my contion statement (WHERE) and my code will look ugly and hard to understand if i just concatenate strings toghether.Rhapsodize
G
16

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

Galitea answered 30/10, 2013 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.