日期:2014-05-17 浏览次数:20622 次
//table方法,用来查询数据库
function table(){
//初始化连库函数,这里$conn是之前实例化的pdo
globle $conn;
//按照条件查询
$sel=$conn->query("select * from my_db order by age");
//返回变量到函数
return $sel->fetchAll();
}
$smarty->assign('table',table());
include("smarty_inc.php");
$con = mysql_connect("127.0.0.1","user","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//$db=new pdo('mysql:127.0.0.1;mysql','user','123456','true');
mysql_query("set names gbk");
mysql_select_db("test",$con);
$result=mysql_query("select * from my_db");
<?php
include("smarty_inc.php");
$con = mysql_connect("127.0.0.1","user","123456");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_query("set names gbk");
mysql_select_db("test",$con);
$result=mysql_query("select * from my_db order by age");
while($row=mysql_fetch_assoc($result)){
$table[]=$row;
}
$smarty->assign('table',$table);
------解决方案--------------------
前台利用smarty的assign赋值给变量。然后利用smarty的section进行循环显示。