Database

概要

データベースへのアクセスに関するクラス


初期設定

データベースサーバを用意し、データベースを予め作成しておく


configディレクトリ配下のdb.phpファイルを編集する

<?php
/**
 * The development database settings.
 */
 
return array(
	'default' => array(
		'type'           => 'mysqli',
			'connection'     => array(
			'hostname'       => 'localhost',
			'port'           => '3306',
			'database'       => 'testdb01',
			'username'       => 'root',
			'password'       => '',
			'persistent'     => false,
		),
		'identifier'   => '`',
		'table_prefix'   => '',
		'charset'        => 'utf8',
		'enable_cache'   => true,
		'profiling'      => false,
	),
);
 
 

簡単な処理で動作を確認

コントローラー
<?php
 
// サンプルコントローラー
class Controller_Sample11 extends Controller
{
	// アクションを省略時のデフォルトの画面
	public function action_index()
	{
		//データベース接続
		$query = DB::select()->from('sample_tbl01')->execute();
 
		//ビューを作成
		$view = View::forge('sample11/index');
 
		// ビューに変数を割り当て
		$view->set('username', 'ほげほげ');
		$view->set('title', 'サンプルテスト');
		$view->set('data', $query->as_array());
 
		// ビューをブラウザに出力
		return $view;
	}
 
}
 
 

ビュー
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title><?php echo $title; ?></title>
	</head>
	<body>
		<table width="100%" border="1">
			<tr>
				<th scope="col">id</th>
				<th scope="col">name</th>
				<th scope="col">biko</th>
				<th scope="col">date</th>
			</tr>
			<?php foreach ($data as $row){ ?>
			<tr>
				<td><?php echo $row['id']; ?></td>
				<td><?php echo $row['name']; ?></td>
				<td><?php echo $row['biko']; ?></td>
				<td><?php echo $row['date']; ?></td>
			</tr>
			<?php } ?>
		</table>
	</body>
</html>
 
 

確認


クラス

DB

DBUtil


サンプル

select

insert

update

delete




最終更新:2013年05月23日 00:34