Home > Article > Backend Development > ThinkPHP connection database operation case analysis
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('THINK_PATH', '../ThinkPHP'); //定义ThinkPHP框架路径(相对于入口文件) define('APP_NAME', 'App'); //定义项目名称 define('APP_PATH', './App'); //定义项目路径 require(THINK_PATH."/ThinkPHP.php"); //加载框架入口文件 App::run(); //实例化一个网站应用实例 ?>
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('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. 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='select' id='user' > <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>
b585974ae3b7dba3039af53a9593f9c4 383eb734b02b508089ba2d78eb4c6f68 93f0f5c25f18dab9d176bd4f6de5d30e 0f3eabac747cdf61a51589c5584ae6e8 b2386ffb911b14667cb8f0f91ea547a7类别输出6e916e0f7d1e588d4f442bf645aedb2f 6c7ddf4d7c0f75469d7a30e3d863fc99 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 34ee0b322842bc45f15af297c0d2d0ed a34de1251f0d9fe1e645927f19a896e8 c4b3641211e3cffd15e5c47153d934c9类别输出b90dd5946f0946207856a8a37f441edf fd273fcf5bcad3dfdad3c41bd81ad3e5 ec161030ee398dd3ae43253f7daa44b6 6d1ab1ffe6592b462a8ccd8e1ccec5ceIDb90dd5946f0946207856a8a37f441edf fe9c64079c19d5b75708035a065e6159类别名称b90dd5946f0946207856a8a37f441edf 9a4095b46dde3329b15d7ede478b32c7添加时间b90dd5946f0946207856a8a37f441edf fd273fcf5bcad3dfdad3c41bd81ad3e5 ad4402d35241c1ed814b7d3dbfde2b80 4477fed78c6029ebd672c138ff2d899a 5000c7c7fe47c9217333200421f30dd6 {$type.id}b90dd5946f0946207856a8a37f441edf 5000c7c7fe47c9217333200421f30dd6 {$type.typename}b90dd5946f0946207856a8a37f441edf 5000c7c7fe47c9217333200421f30dd6 {$type.dates}b90dd5946f0946207856a8a37f441edf fd273fcf5bcad3dfdad3c41bd81ad3e5 0c2cdd09f5f1098aaa1356781bc5fc07 f16b1740fad44fb09bfe928bcc527e08 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
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!