zoukankan      html  css  js  c++  java
  • drupal7使用数据库api db_query需要注意的地方

    写自定义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之,在官网问答里面的到了启发:


    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();
  • 相关阅读:
    关于开发BugRaid的想法(2)
    杭州.net俱乐部第一次见面会情况
    关于Bug管理的过程
    关于开发BugRaid的想法(1)
    变态的VS2005 SP1
    vs2003 sp1 download url
    proxy software
    敏捷开发
    Enterprise Library文档
    ETL
  • 原文地址:https://www.cnblogs.com/hui314/p/drupal_db_query_notice.html
Copyright © 2011-2022 走看看