mongoDB接続をclassを用いて書きます.
mySQLを接続するのは他のサイトとかにたくさんありますしね.
class Okome{ var $mongo; var $db; var $col; function __construct($selectDB,$selectCollection){ $this->selectDB = $selectDB; $this->selectCollection = $selectCollection; $this->mongo = new Mongo(); $this->db = $this->mongo->selectDB($this->selectDB); $this->col =$db->selectCollection($this->selectCollection); } function showDB(){ $col = $this->col; $obj = $col->find(); print '==========new DATA========== '; foreach($obj as $id => $text){ var_dump($text); print ' '; } print ' '; $count = $col->count(); print $count.'個 '; foreach($obj as $uho =>$hoge){ foreach($hoge as $id => $con){ if($id !== '_id'){ print $id.':'.$con.' '; } } } } function addNewData($name,$grade,$birth){ $doc = array($name,$grade,$birth); print_r($doc); $this->col->insert($doc); print ' '; $this->showDB(); } function deleatAllData(){ $this->col->drop(); } }
コンストラクタも使えるので,必要な基本情報は最初にコンストラクタからメンバ変数に入れます.
あとはそれぞれのfunctionでやりたい事やればOK.
クラスの外からはこんな感じで.
$okome = new Okome('okome','friend'); $okome->showDB();
って感じで,3日目おしまい.
明日は,3年生のトップバッター!たのしみ!
0 件のコメント:
コメントを投稿