0
1

More than 5 years have passed since last update.

(入門) CodeIgniter 2 でdatabase接続

Last updated at Posted at 2016-12-01

参考

インストール

git clone https://github.com/tukiyo/docker-php5.6-apache
cd docker-php5.6-apache

sh files/get_codeigniter2.sh
chmod -R 777 app/tmp
docker-compose up

model

  • application/config/database.php の修正は終わっていること。
  • select id,name FROM shopを行いたい。
touch application/models/shop.php
application/controllers/Welcome.php
<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index() {

        $this->load->model('Shop');
        $res = $this->Shop->get_info();
        echo "<pre>";
        var_dump($res);
        //$this->load->view('welcome_message');
    }
}
application/models/Shop.php
<?php

class Shop extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_info() {

        $this->db->select('id,name');
        $this->db->from('shop');
        $query = $this->db->get();
        //echo $this->db->last_query();

        $res = null;
        if ($query->num_rows() > 0) {
            $res = $query->result();
        }
        return $res;
    }

}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1