温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用ThinkPHP怎么连接数据库

发布时间:2021-03-19 17:01:58 来源:亿速云 阅读:151 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关使用ThinkPHP怎么连接数据库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一 代码

1、完成入口函数的编写

<?php
define('THINK_PATH', '../ThinkPHP');    //定义ThinkPHP框架路径(相对于入口文件)
define('APP_NAME', 'App');       //定义项目名称
define('APP_PATH', './App');        //定义项目路径
require(THINK_PATH."/ThinkPHP.php");  //加载框架入口文件
App::run();               //实例化一个网站应用实例
?>

2、完成控制器的编写

<?php
header("Content-Type:text/html; charset=utf-8");  //设置页面编码格式
class IndexAction extends Action{
  public function index(){
    $db_dsn="mysql://root:root@127.0.0.1:3306/db_database30";    //定义DSN
    $db = new Db();                       //执行类的实例化
    $conn=$db->getInstance($db_dsn);               //连接数据库,返回数据库驱动类
    $select=$conn->query('select * from think_user');      //执行查询语句
    $this->assign('select',$select);       // 模板变量赋值
    $this->display();              // 指定模板页
  }
  public function type(){
    $dsn = array(
      'dbms'   => 'mysql',
      'username' => 'root',
      'password' => 'root',
      'hostname' => 'localhost',
      'hostport' => '3306',
      'database' => 'db_database30'
    );
    $db = new Db();
    $conn=$db->getInstance($dsn);              //连接数据库,返回数据库驱动类
    $select=$conn->query('select * from think_type');      //执行查询语句
    $this->assign('select',$select);       // 模板变量赋值
    $this->display('type');             // 指定模板页
  }
}
?>

3、完成模板编写

<!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=utf-8" />
<title>用户信息输出</title>
<link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>
 </tr>
 <tr class="title">
  <td bgcolor="#FFFFFF" width="44">ID</td>
  <td bgcolor="#FFFFFF" width="120">名称</td>
  <td bgcolor="#FFFFFF" width="223">地址</td>
 </tr>
 <volist name='select' id='user' >
 <tr class="content">
  <td bgcolor="#FFFFFF">&nbsp;{$user.id}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$user.user}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$user.address}</td>
 </tr>
 </volist>
</table>
</body>
</html>
<!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=utf-8" />
<title>类别输出</title>
<link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">类别输出</td>
 </tr>
 <tr class="title">
  <td bgcolor="#FFFFFF" width="44">ID</td>
  <td bgcolor="#FFFFFF" width="120">类别名称</td>
  <td bgcolor="#FFFFFF" width="223">添加时间</td>
 </tr>
 <volist name='select' id='type' >
 <tr class="content">
  <td bgcolor="#FFFFFF">&nbsp;{$type.id}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$type.typename}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$type.dates}</td>
 </tr>
 </volist>
</table>
</body>
</html>

二 运行结果

使用ThinkPHP怎么连接数据库

使用ThinkPHP怎么连接数据库

以上就是使用ThinkPHP怎么连接数据库,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI