php框架thinkphp3.2怎么读取数据库内容

有一个tpshow数据库,库里面有一个show表,求代码
2024-11-10 07:09:26
推荐回答(2个)
回答1:

先找到config.php文件,如图:

然后打开配置文件并在其中配置数据库的信息,如下代码:

return array(
//'配置项'=>'配置值'
'DB_TYPE'               =>  'mysql',     // 数据库类型
'DB_HOST'               =>  '127.0.0.1', // 服务器地址
'DB_NAME'               =>  'tpshow',          // 数据库名
'DB_USER'               =>  'root',      // 用户名
'DB_PWD'                =>  'root',          // 密码
'DB_PORT'               =>  '3306',        // 端口
'DB_PREFIX'             =>  'tp_'    // 数据库表前缀
);

然后在应用的Home的Controller中,如下图的文件

namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $db = M("show"); // 实例化show对象
    $data = $db->find();//读取一条数据
    dump($data);//打印数据
    }
}

回答2:

$show = M("show"); // 实例化show对象
// 查找status值为1数据
$data = $show->where('status=1')->find();
dump($data);