>[info]新建 models/Article.php 文件,内容为(数据库密码请自行更改):
建议使用mysqli或者pdo进行数据库操作,由于新版本的php使用mysql会报错
~~~
<?php
/**
* Article model
*/
class Article
{
public static function first() {
$connect = mysqli_connect("localhost", 'root', 'password', 'database') or die('连接失败'.mysqli_connect_error());
mysqli_query($connect,"set names utf8");
$query = mysqli_query($connect, "select * from table limit 1,2");
if(mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)){
echo $row['title']."<br/>";
echo $row['author']."<br/>";
echo $row['tags']."<br/>";
echo $row['description']."<br/>";
echo "<br/><hr/>";
}
}
mysqli_close($connect);
}
}
~~~
>[info]修改 controllers/HomeController.php 文件:
~~~
<?php
/**
* HomeController
*/
class HomeController extends BaseController
{
public function home(){
Article::first();
}
}
~~~
>[warning]刷新,这时候会得到 Article 类未找到的信息,因为我们没有更新自动加载配置:
~~~
composer dump-autoload
~~~
再次刷新网页:
![](https://box.kancloud.cn/7f6218d248acfac3678b16d462618c7f_745x303.png)
这个时候model数据库操作实现!