データベースへのアクセス方法

Events happening in the community are now at Drupal community events on www.drupal.org.
osonoi's picture

PHPのページをつくりそこからDrupalデータベースのデータを利用し表示したいのですがどのようなやり方があるのでしょうか?

Comments

aiwata55's picture

モジュールを作成する場合のすごく簡単な説明ですが...

hook_menu() を使って PHP + HTML のページを作ります。
作ったページ内に,SQL クエリーを埋め込みます。

これで,データベースから引っ張って来たデータを表示するページを Drupal サイト内に追加できます。

まずは,hook_menu() を使って,適当な内容の(Hello World とか)ページを作り,そこへのリンクをメニューに登録する。というところから(データベース関連と切り分けて)始めるといいと思います。

きちんと詳しく説明しようと思うと,ものすごく長くなってしまって本の2〜3章分ぐらいは簡単に行ってしまいます。もしお持ちなら Pro Drupal Development の4章 The Menu System,5章 Working with Databases,20章 Writing Secure Code を参考にしてみてください。(この章番号は英語版のものです。日本語版はもしかしたら違うかもしれません)

とりあえず,api.drupal.org の関連ページへのリンクを貼っておきます。
http://api.drupal.org/api/function/hook_menu/6
http://api.drupal.org/api/group/menu/6
http://api.drupal.org/api/group/database/6

分からなければ,遠慮なくまた訊いてみてください。


Aki Iwata
FOREST & trees

Views

aiwata55's picture

言い忘れましたが,もし Views でまかなえるクエリーだったら,Views を使うのがもっとも簡単かつセキュリティ上安全な方法だと思います。

Views を使えば,クエリーを作ってくれるだけでなく,そのクエリーの結果を含むページを作ったり,それをメニューに登録したり,アクセス制限を設けたりといったことが可能です。

Views を使うとオーバーヘッドが増えて処理が遅いという意見もあるようですが,個人的には無視できる程度だと思いますし,また別の方法(サーバーのマシンスペックを上げる,キャッシュなどのパフォーマンス向上のための環境構築を行うなど)で挽回できる(あるいは挽回すべき)と思います。

http://drupal.org/project/views


Aki Iwata
FOREST & trees

phpプログラマの視点から見ると

xbro's picture

phpプログラマの視点から見ると、私が理解してる限りDrupalのデータベースアクセスはphpの一番ベーシックな、そのページからその都度、ページ内のクエリを実行する方法が主なやり方と理解しています(データベースのfunction専門の.incファイルとか簡単に作成して使用するなどは無理だと思います、モジュールを作るのですかね?)。ようするに他のページなどでも同じクエリ実行でも、また同じクエリをそこで直接書き込むと理解しています。

一応それに対して、それでもなぜDrupalの考え方が良いのかいう議論は、確かにそのページで必要なクエリのコードだけが実行されるのが、一番早いので(長い.incなどすべてをシステムが読むより)、なるほどと思った時期がありましたが、いまだにDrupalであまりデータベースアクセスするクリエイティブなシステムを実験的に作る気は沸いてきません。

phpのObjectOriented技法を独自で使用するのにDrupal初心者として解らないDrupalモジュール作りをする事は、データベースアクセスごときと思う事に学習する意欲が出ず、これは何とかして欲しいと本当以前思ってた時期がありました。

DBモジュール作り

xbro's picture

ちなみに独自の.incファイルから独自のFunctionを呼んで、自由自在にデータベースをアクセスして、それを検索エンジン式に自由に表示できるDrupalモジュールの作り方をstep-by-stepでどこかに情報や、勉強会があればとてもありがたいです。

fyi, if someone could show me step by step (since I'm a Drupal newbie) on how to construct a Drupal module that can read query functions from a long/multiple include files and display the results in page sets easily, I would very much appreciate it as it would make my experience with Drupal very fun. coding the queries on a page basis and having the queries all over the place is something that makes Drupal very unattractive to a PHP programmer in my opinion (surely I'm not the first person to mention it but I haven't found any useful information with regards to this in the past). Ideally I'd like this module to display a search text box too. does this trying to integrate php object oriented techniques into a Drupal site even make sense in Drupal?

Sorry to hear that you're not

Antoine Lafontaine's picture

Sorry to hear that you're not enjoying your Drupal experience.

If you want to see some Drupal/OOP action, have a look at the views (ver 2 + ) code.

If you want to do some custom queries, I believe it is worth learning how to use the views api and use it to handle your custom (module) queries. It offers various advantages over building your own custom solution, but basically it leverages the work of many people involved with creating web applications. You could add to the list of advantages: higher security, reusability, interoperability with other Drupal modules, the ability to override the queries defined in code, access control, caching...

Hope this helps your overcome your problem.

Aiwata, Antoine thanks

xbro's picture

Aiwata, Antoine thanks for the Views feedback.

Thinking about this, actually my php practice includes placing all my query results display code in one .inc file and calling them to start the db calls from another db .inc file . So I think learning the views API is not that far off. Sounds interesting to find out if there's a powerful Drupal db solution waiting to come out.

ありがとうございます。

osonoi's picture

そうでした本をみればよかったんです。ものぐさな私に”大渇”です。
今度のmeetupまでにはできるようにがんばります。
お休み中ありがとうございました。

dokumori's picture

データをDBから呼び出して表示するのは一見簡単ですが、正しいフォーマットで安全に表示するには少し注意が必要です。不特定多数のユーザーの投稿したデータを表示する場合は特に気をつけなければなりません。もしjavascriptがアウトプットに含まれていて、それが実行されてしまうようでは、クロスサイトスクリプティング(XSS)の被害に遭う可能性が生じます。

Drupalはユーザーの投稿したデータをサニタイズせずにDBに格納するので、アウトプットの際にサニタイズする必要があります。Drupalは独自にサニタイズ関数を持っているので、それらを使うことをお勧めします。
どの条件でどのフィルターを使うべきかを分りやすく表したチャートがあるので参照してください:
http://crackingdrupal.com/blog/greggles/drupal-text-filtering-decision-c...

XSSは多くのプログラミングエラーのなかで堂々一位にランクインしています:http://cwe.mitre.org/top25/ 
XSSはセキュリティチームに報告される問題のなかでもXSSがトップを占めています。ついうっかりサニタイズしないままアウトプットしてしまうのは分からないでもないですが、全くこのリスクを考慮していない開発者も少なくないのが実状です。ぜひ気をつけてくださいね。

日本 コミュニティ: Drupal Japan User Group

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: