PHP--TP开发模式

0、开启调试模式(WWW\tp5\application\config.php)

    'app_debug' =>true,

1、连接数据库(...database.php)

     // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => '127.0.0.1',
    // 数据库名
    'database'        => 'testphp',
    // 用户名
    'username'        => 'root',
    // 密码

    'password'        => 'root',

2、控制器中书写代码(...application\index\controller\Index.php)

    namespace app\index\controller;
    //引入系统数据类
    use think\Db;
    //引入系统控制类
    use think\Controller;
    class Index extends Controller
    {
        public function index()
        {
    //从数据库中读取数据
    $data=Db::table('user')->select();
    //分配数据给页面
    $this->assign('data',$data);
    //加载页面
    return view();
        }

    }

3、页面中(...index\view\index\index.html)

    {volist name="data" id="value"}

{$value.id}
{$value.name}
{$value.pass}

    {/volist}

二:数据库 增删改查

namespace app\index\controller;

use think\Db;

class Index
{
    public function index()
    {
        return '

:)

ThinkPHP V5
十年磨一剑 - 为API开发设计的高性能框架

[ V5.0 版本由 七牛云 独家赞助发布 ]
'
; } //查询 public function query(){ $date=Db::Query("select * from user where id=?",[2]); dump($date); echo Db::getLastSql(); } //增 public function insert(){ //$data=Db::execute("insert into user value(null,'user1','321')"); //$data=Db::execute("insert into user value(null,?,?)",['user2','12345']); $data=Db::execute("insert into user value(null,:name,:pass)",['name'=>'user3','pass'=>'12345']); dump($data); } //删除 public function deletes(){ //$data=Db::execute("delete from user where id=?",[3]); //$data=Db::execute("delete from user where id=2"); $data=Db::execute("delete from user where id=:id",['id'=>8]); dump($data); } //修改 public function update(){ //$data=Db::execute("update user set pass=? where id=?",["888",9]); $data=Db::execute("update user set pass=? where id=?",["666",9]); $data=Db::execute("update user set pass=:pass where id=:id",['pass'=>"555",'id'=>10]); dump($data); } }

你可能感兴趣的:(PHP)