ThinkPHP connection database operation case analysis

php中世界最好的语言
Release: 2023-03-26 11:36:01
Original
1793 people have browsed it

This time I will bring you ThinkPHPConnect to database operation case analysis, what are the precautions for ThinkPHP connectionDatabase operation, the following is a practical case, Let’s take a look.

One code

1. Complete the writing of the entry function

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

2. Complete the writing of the controller

<?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(&#39;select * from think_user&#39;);      //执行查询语句
    $this->assign(&#39;select&#39;,$select);       // 模板变量赋值
    $this->display();              // 指定模板页
  }
  public function type(){
    $dsn = array(
      &#39;dbms&#39;   => &#39;mysql&#39;,
      &#39;username&#39; => &#39;root&#39;,
      &#39;password&#39; => &#39;root&#39;,
      &#39;hostname&#39; => &#39;localhost&#39;,
      &#39;hostport&#39; => &#39;3306&#39;,
      &#39;database&#39; => &#39;db_database30&#39;
    );
    $db = new Db();
    $conn=$db->getInstance($dsn);              //连接数据库,返回数据库驱动类
    $select=$conn->query(&#39;select * from think_type&#39;);      //执行查询语句
    $this->assign(&#39;select&#39;,$select);       // 模板变量赋值
    $this->display(&#39;type&#39;);             // 指定模板页
  }
}
?>
Copy after login

3. Complete template writing

<!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=&#39;select&#39; id=&#39;user&#39; >
 <tr class="content">
  <td bgcolor="#FFFFFF"> {$user.id}</td>
  <td bgcolor="#FFFFFF"> {$user.user}</td>
  <td bgcolor="#FFFFFF"> {$user.address}</td>
 </tr>
 </volist>
</table>
</body>
</html>
Copy after login
<!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=&#39;select&#39; id=&#39;type&#39; >
 <tr class="content">
  <td bgcolor="#FFFFFF"> {$type.id}</td>
  <td bgcolor="#FFFFFF"> {$type.typename}</td>
  <td bgcolor="#FFFFFF"> {$type.dates}</td>
 </tr>
 </volist>
</table>
</body>
</html>
Copy after login

Second running results

## I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

PHP move_uploaded_file() function practical case explanation

thinkPHP controller variable display steps in the template Detailed explanation

The above is the detailed content of ThinkPHP connection database operation case analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!