mysql> insert into test_1(id,name) values(1,'test'),(2,'test2');
Query OK, 1 row affected (0.05 sec)
//レコードの確認
mysql> select * from test_1;
+------+-------+
| id | name |
+------+-------+
| 1 | test |
| 2 | test2 |
//プログラム
<?php
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');
//sql文の作成
$sql = $dbh->prepare("SELECT * FROM test_1");
$data = array();
$rs = $dbh->execute($sql);
//配列へ挿入
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
$data[]=array("id" => $row['id'],"name" => $row['name']);
}
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート部分
<h1>{$msg}</h1>
{foreach from=$data item=datum}
<p>{$datum.id}</p>
<p>{$datum.name}</p>
{/foreach}
Query OK, 1 row affected (0.05 sec)
//レコードの確認
mysql> select * from test_1;
+------+-------+
| id | name |
+------+-------+
| 1 | test |
| 2 | test2 |
//プログラム
<?php
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');
//sql文の作成
$sql = $dbh->prepare("SELECT * FROM test_1");
$data = array();
$rs = $dbh->execute($sql);
//配列へ挿入
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
$data[]=array("id" => $row['id'],"name" => $row['name']);
}
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート部分
<h1>{$msg}</h1>
{foreach from=$data item=datum}
<p>{$datum.id}</p>
<p>{$datum.name}</p>
{/foreach}
0 件のコメント:
コメントを投稿