smarty

本文详细介绍了如何使用PHP Smarty模板引擎与数据库进行交互,包括数据库连接、SQL执行、结果集处理、变量赋值及展示,以及如何利用Smarty模板进行页面渲染。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<?php
include("./init.inc.php");
$a=10;
$tpl->assign("title","this is php various");
$tpl->assign("title1",$a);
$tpl->assign("title2",true);

 //从数据库smarty_var,表user 获取
 //连接数据库使用内置类mysqli
 //不需要include(require),直接用
 //php.ini开启extension php_myqli.dll
 //重启apache
 //mysqli类:连接
 //第一步:连接数据库

 $mysqli = new mysqli("localhost","root","123","smarty_var");
 
 //第二步:执行SQL命令
 //insert delete update:返回影响的行数
 //select:返回结果集
 //mysql_query()
 //$mysqli->query()
 //mysqli_result类
 //结果集对象,创建该对象不能new   mysqli_result,而是通过调用 //mysqli的query方  法直接返回结果集对象


  $result = $mysqli->query("select * from user");
 
 //第三步:使用该对象获取结果集中的(关联、索引)数组
 // mysql_fetch_array(assoc row)
 //$result->fetch_assoc( row)
 
 //索引数组分配
   $row = $result->fetch_row();
   print_r($row);
  
//关联数组分配
// $row = $result->fetch_assoc();
//  print_r($row);
//user表里字段包括(id,name,age,email)
//$tpl->assign("id",$row['id']);
//$tpl->assign("name",$row['name']);
//$tpl->assign("age",$row['age']);
//$tpl->assign("email",$row['email']);


//索引数组可以一次性分配
$tpl->assign("row",$row);
 
 
 
//自定义数组

$tpl->assign("array1",array("1","2","3"));
$tpl->assign("array2",array(array("a","b"),array("c","d")));
$tpl->assign("array3",array("one"=>"one","two"=>"two"));
$tpl->assign("array4",array(array("two"=>"bb"),"one"=>array("aa"),array("three"=>"cc")));


//对象的分配
class Person{
var $name;
var $age;
public function __construct($name,$age){
$this->name = $name;
$this->age = $age;
}
function say(){
  return $this->name."的年龄是".$this->age;
}
}

//分配变量

$tpl->assign("person",new Person("zs",20));


//数学运算
$tpl->assign("num1",10);
$tpl->assign("num2",20);

 

 
 

$tpl->display("a.html");
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值