Drupal Provide us nice API function to print the query which is built using db_select() in a programmatical way
If you have not Devel Module installed
<?php$query = db_select('node', 'n'); $query->join('users', 'u', 'n.uid = u.uid'); //JOIN node with users $query->groupBy('u.uid');//GROUP BY user ID $query->fields('n',array('title','created'))//SELECT the fields from node ->fields('u',array('name'))//SELECT the fields from user ->orderBy('created', 'DESC')//ORDER BY created ->range(0,2);//LIMIT to 2 records // Print Query as string print $query->__toString(); // or // Second way // echo (string)$query; // print arguments in query echo '?>
<?php';print_r($query->arguments()); echo '?>
<?php'; ?> ?>
If you have Devel Module installed
You can use dpq() to display the query, and dpr() to display the result.
<?php$query = db_select('users','u'); $query->fields('u'); $query->condition('u.uid', 1042); $result = $query->execute()->fetchAll(); dpq($query); // Display the query. dpr($result); // Display the query result. ?>