写自定义module时候需要从数据库检索数据,用到了数据库的api,用了下面的sql:
$record = db_query("SELECT 'sampledate', 'time' FROM {table_name} WHERE uid = :uid", array(':uid' => $uid))->fetchObject();
结果var_dump($record);得到的结果为
object(stdClass)#221 (2) { ["sampledate"]=> string(10) "sampledate" ["heartrate"]=> string(9) "heartrate" }
思来想去,“百撕不得骑姐”,无奈只好google之,在官网问答里面的到了启发:
You must use single quotes
This is new in Drupal 7. Although MySQL doesn't care about single or double quotes, other SQL engines do. Works: SELECT delta FROM {block} WHERE status = 0 AND module = 'tracker' Does not work: SELECT delta FROM {block} WHERE status = 0 AND module = "tracker"
咋一看之下,两个SQL并无差异。只是‘ 与 “ 的分别。原来drupal7里面SQL要使用single quotes,于是把SQL该了一下,去掉 ‘ (单引号),得到的结果就正常了:
$record = db_query("SELECT sampledate, time FROM {table_name} WHERE uid = :uid", array(':uid' => $uid))->fetchObject();