首页 > PHP–love > thinkphp 数据库简单上手

thinkphp 数据库简单上手

昨天就把今天要用的数据库创建SQL语句给写好了哈.

今天先往里插入一条数据,按教程的写法,看一下都有哪些我感兴趣的东西.

insert into think_form values (1,'it\'s a demo data','xiaoxiaozi',1212724876,0,1,'yufulong@gmail.com');

现在表有了,表里也有内容了,那么怎么用MODEL把内容读出来呢?

首先,在配置文件里一定要把数据库的基本信息写上。为啥?你不写的话咋联数据库啊?

~/www/thinkphp/MyApp/Conf
vim config.php
if (!defined('THINK_PATH')) exit();
return  array(
'DB_TYPE'       => 'mysql',
'DB_HOST'       => 'localhost',
'DB_NAME'       => 'myapp',
'DB_USER'       => 'root',
'DB_PWD'        => 'xiaoxiaozi',
'DB_PORT'       => '3306',
'DB_PREFIX'     => 'think_',
);
cd ../Lib/Model/
vim FormModel.class.php
class FormModel extends Model
{

}

就这样,我们的MODEL就建好了。命名嘛,当然要符合TP的规范了。。

接下来,更改IndexAction.class.php,使其可以使用FormModel.class.php里定义的数据库对象啊。

class IndexAction extends Action{
    public function index(){
        $form = D('Form')->findall();
        var_dump($form);
        exit;
    }
}

这样啊,这个表里的每一行都出来了,结果如下:

/*
string(5) "MyApp" array(1) { [0]=>  array(7) { ["id"]=>  string(1) "1" ["title"]=>  string(16) "it's a demo data" ["content"]=>  string(10) "xiaoxiaozi" ["create_time"]=>  string(10) "1212724876" ["update_time"]=>  string(1) "0" ["status"]=>  string(1) "1" ["email"]=>  string(18) "yufulong@gmail.com" } }
*/

啥?你想问findall方法在哪定义的?我咋知道有这么个方法?

好吧,我承认,我这还是抄的教程里的例子,没办法,我是懒人啊。。

但是这个我还真的找了一下,呵呵。

 cd  /home/simaopig/www/thinkphp/ThinkPHP/Lib/Think/Core

在Model.class.php里面就是所有Model继承的父类Model的定义了。

刚才的方法findall()就在其中啦。

  1. 荒野无灯 四 23rd, 2009 @ 14:01 | #1

    呵呵,你终于开始弄thinkphp了

  2. simaopig 四 23rd, 2009 @ 16:20 | #2

    @荒野无灯
    哈,是啊。既然下定决心学了。就从现在开始好了。。 :cool:

评论提交中, 请稍候...

留言

可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks