It took me a huge amount of time to figure out a working Drupal 7, "Allowed values PHP code" + sql element. I never did find a working example (the only examples I could find were for D6, so I had to dig through the db_query API + CCK documentation to figure out how to put it all together.
While I don't necessarily think it is the CCK team's job to have to explain the D7 core changes, it would make things a ton easier for people to leverage the CCK module + D7 query syntax. I've included a few basic examples below that if wanted, and if they pass muster, could be used in the documentation.
The example below will return an associative array of fields from the result set from a simple query (no parameters):
$sql = "SELECT uid, name FROM {users}";
return db_query($sql, array())->fetchAllKeyed();
The example below will return an associative array of fields from the result set from a simple query (with one parameter):
$myUser = user_load_by_name("John M");
$sql = "SELECT uid, name FROM {users} WHERE uid = :uid";
return db_query($sql, array(':uid' => $myUser->uid))->fetchAllKeyed();
The example below shows how to loop through individual rows of the result set and build the associative array by hand:
$sql = "SELECT uid, name FROM {users} where uid > 0";
$rows = db_query($sql, array());
foreach ($rows as $row) {
$options[$row->uid] = t($row->name);
}
return $options;
Some items of note for D7 and queries:
For static queries, all table names must be wrapped in {}
All selectors in a query must use single tick (') not quote ("). So:
GOOD: SELECT uid, name FROM {users} WHERE name = 'John M'
BAD: SELECT uid, name FROM {users} WHERE name = "John M"
Some links of note:
Static queries (http://drupal.org/node/310072)
Excellent examples of the various fetch parameters (http://api.drupal.org/api/drupal/includes--database--database.inc/functi...)