smarty手记3--连接数据库
模板文件Mysql.tpl:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>Smarty--mysql数据库/关联数组使用</title>
<style type="text/CSS">
body{width:1000px;margin:0 auto;}
h3,th{font-weight:nORMal;}
#studentinfo h3
{
color:#674B33;
}
table
{
border-spacing:1px;
}
td,th
{
padding:5px;
}
#studentinfo
{
font-size:12px;
}
#studentinfo table
{
background-color:#F9EDE3;
}
#studentinfo th
{
background-color:#B1733B;
color:#fff;
}
#studentinfo td
{
background-color:#EECAAA;
color:#54402E;
text-align:center;
}
</style>
</head>
<body>
<div id="studentinfo">
<h3>学生信息:</h3>
<table>
<tr>
<th style="width:100px;">姓名</th><th style="width:50px;">年龄</th><th style="width:200px;">专业</th>
</tr>
<{section name=index loop=$students}>
<tr>
<td><{$students[index].name}></td><td><{$students[index].age}></td><td><{$students[index].major}></td>
</tr>
<{/section}>
</table>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>Smarty--mysql数据库/关联数组使用</title>
<style type="text/CSS">
body{width:1000px;margin:0 auto;}
h3,th{font-weight:nORMal;}
#studentinfo h3
{
color:#674B33;
}
table
{
border-spacing:1px;
}
td,th
{
padding:5px;
}
#studentinfo
{
font-size:12px;
}
#studentinfo table
{
background-color:#F9EDE3;
}
#studentinfo th
{
background-color:#B1733B;
color:#fff;
}
#studentinfo td
{
background-color:#EECAAA;
color:#54402E;
text-align:center;
}
</style>
</head>
<body>
<div id="studentinfo">
<h3>学生信息:</h3>
<table>
<tr>
<th style="width:100px;">姓名</th><th style="width:50px;">年龄</th><th style="width:200px;">专业</th>
</tr>
<{section name=index loop=$students}>
<tr>
<td><{$students[index].name}></td><td><{$students[index].age}></td><td><{$students[index].major}></td>
</tr>
<{/section}>
</table>
</div>
</body>
</html>
PHP文件smarty4.php
<?php
require_once "../smarty/Smarty.class.php";
$smarty=new Smarty();
$smarty->template_dir="../php/tmps";
$smarty->compile_dir="../php/tmps_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$dsn = 'mysql:dbname=test;host=127.0.0.1';
try{
$dbh= new PDO($dsn, 'root', '123456');
}catch(PDOException $e){
echo "数据库连接失败:".$e->getMessage();
}
$dbh->exec("set names gbk");
$rs=$dbh->query("SELECT * FROM student");
$students=array();
while($row=$rs->fetch(PDO::FETCH_ASSOC)){
array_push($students,$row);
}
$db = null;
$smarty->assign("students",$students);
$smarty->display("mysql.tpl");
?>
require_once "../smarty/Smarty.class.php";
$smarty=new Smarty();
$smarty->template_dir="../php/tmps";
$smarty->compile_dir="../php/tmps_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$dsn = 'mysql:dbname=test;host=127.0.0.1';
try{
$dbh= new PDO($dsn, 'root', '123456');
}catch(PDOException $e){
echo "数据库连接失败:".$e->getMessage();
}
$dbh->exec("set names gbk");
$rs=$dbh->query("SELECT * FROM student");
$students=array();
while($row=$rs->fetch(PDO::FETCH_ASSOC)){
array_push($students,$row);
}
$db = null;
$smarty->assign("students",$students);
$smarty->display("mysql.tpl");
?>
结果:
注:数据库
相关文章