TP架構---thinkphp查詢和添加資料

來源:互聯網
上載者:User

標籤:can   切換   mit   url   space   ble   count()   function   distinct   

查詢

<?phpnamespace Admin\Controller;use Think\Controller;class MainController extends Controller{    public function showList()    {        echo "大蘋果商城";    }        public function test()    {        //資料訪問        //造模型對象        //$nation = D("Nation");//串連資料庫中的Nation表                //查詢        //$a = $nation->select();//查詢所有,返回關聯陣列,也是二維數組。        //$a = $nation->select("n001,n002,n003");//通過主索引值查,注意寫法。                //$a = $nation->find();//查一條資料,返回一維數組。        //$a = $nation->find("n001");//按照條件查詢                //$a = $nation->where("name=‘漢族‘or name=‘回族‘")->select();//where()只能寫查詢條件。select才會輸出,select返回的是二維數組。這種操作稱為連貫操作                //$a = $nation->table("info")->select();//table切換其它表格式查詢表格資訊。上面雖然是連結的Nation表,這裡切換到info表格。                //$a = $nation->field("code")->select();//查詢指定欄位,這裡查詢的是code列,多個欄位中間用,分隔。                //$a = $nation->order("code desc")->select();//code按照降序排列                //$a = $nation->limit(3)->select();//查詢前3條資料,limit分頁查詢。        //$a = $nation->limit(3,4)->select();//跳過3條顯示4條                //$a = $nation->page(2,3)->select();//page中第一個參數是第幾頁,第二個參數是幾條資料。這裡是取第2頁的3條資料。                //$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->select();//分組查詢                //$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->having("avg(price)>50")->select();//having條件查詢                //$a = $nation->field("Info.code,Info.name as ‘name‘,nation.name as ‘民族‘")->join("Info on Nation.code=Info.Nation")->select();//用join串連的時候前面的field的列要加上別名,不然會出問題。                //$a = $nation->table("car")->distinct(true)->field("brand")->select();//distinct去重                //$a = $nation->where("code=‘n003‘")->getField("name");//只能寫列名,擷取某一列的值。                //$a = $nation->count();//也可以放在連貫操作的最後用,求出資料量。                //$a = $nation->table("car")->max("price");                //使用原生的查詢方法        //$sql = "select * from nation";        //$a = $nation->query($sql);//查詢語句還是按照原來的調用方法。                //$sql = "update nation set name=‘矮人族‘ where code=‘n001‘";        //$a = $nation->execute($sql);//增、刪、改語句用execute調用。                //var_dump($a);    }}

添加

<?phpnamespace Admin\Controller;use Think\Controller;class MainController extends Controller{    public function showList()    {        echo "大蘋果商城";    }        public function test()    {        $nation = D("Nation");//串連資料庫中的Nation表                //資料添加        //1.使用數組添加,數組必須為關聯陣列,索引為列名。        //$attr = array("Code"=>"n090","Name"=>"蟲族");        //$nation->add($attr);                //2.AR方式實現資料添加        //$nation->Code = "n030";        //$nation->Name = "朝鮮族";        //$nation->add();        //$this->redirect(‘New/category‘, array(‘cate_id‘ => 2), 5, ‘頁面跳轉中...‘);//上面的用法是停留5秒後跳轉到New模組的category操作,並且顯示頁面跳轉中字樣,重新導向後會改變當前的URL地址。    }            public function zhuCe()    {        //3.自動收集表單(非常好用的一點)        //要實現2個邏輯        //(1)顯示註冊頁面        //(2)向資料庫新增內容        if(empty($_POST))        {            $this->show();        }        else        {            //自動收集表單            $n = D("Nation");            $n->create();//自動收集表單的寫法。前提是必須有post數組才能用create方法收集到。表單的name必須是資料庫的列名。                        //$n->sex = "";//需要處理的列單獨拿出來處理,比如nation表中男女存的是布爾型資料1和0,而使用者在填寫表單的時候輸入的是男或女,在這裡單獨修改一下,男=1,女=0.                        $z = $n->add();            if($z)//success和error方法的第一個參數表示提示資訊,第二個參數表示跳轉地址,第三個參數是跳轉時間(單位為秒)。            {                $this->success("添加成功","zhuCe");//如果成功先輸出這一句話,再跳轉到zhuCe方法。            }            else            {                $this->error("添加失敗");//錯誤頁面的預設跳轉頁面是返回前一頁,通常不需要設定            }        }            }        public function canShu($b=0)//注意不能使用a、c、m、s傳值,這裡給變數一個預設值,防止報錯。    {        //$id = $_GET["id"];        //echo "$id";        echo $b;    }}

 

TP架構---thinkphp查詢和添加資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.