再說“使用CI操作oracle 10g的單表增刪改查”

來源:互聯網
上載者:User

標籤:使用   io   資料   for   ar   cti   資料庫   sql   

--Control中的UseOra.php

 

 

<?php

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

class UseOra extends CI_Controller
{

    public function index()
    {
        echo ‘歡迎使用測試CI使用Oracle 10g 的功能,您可以在地址欄中使用如下的函數參數:<br>‘;
        echo ‘========================================================================<br>‘;
        echo ‘selectData<br>‘;
        echo ‘selectDataWitchParm($deptno)<br>‘;
        echo ‘addData($deptno,$deptname,$deptloc)<br>‘;
        echo ‘delData($deptno)<br>‘;
        echo ‘updateData($detpno,$loc)<br>‘;
        echo ‘arSelectData()    使用ActiveRecord方式<br>‘;
        echo ‘arSelectDataWithParam($deptno)   使用ActiveRecord方式<br>‘;
        echo ‘========================================================================<br>‘;
        echo ‘注意:已經在autoload.php中自動載入了資料庫通用類了!<br>‘;
        echo ‘========================================================================<br>‘;
        echo ‘使用的資料庫平台是:‘ . $this->db->platform() . ‘<br>版本是:‘ . $this->db->version();
    }

    public function selectData()
    {
        $sql = ‘select deptno,dname,loc from dept order by deptno‘;
        $res = $this->db->query($sql);
        $depts = $res->result();
        foreach ($depts as $dept)
        {
            echo ‘編號:‘ . $dept->DEPTNO . ‘<br>‘;
            echo ‘名稱:‘ . $dept->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $dept->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

    public function selectDataWitchParm($deptno)
    {
        $sql = ‘select deptno,dname,loc from dept where deptno=? order by deptno‘;
        $res = $this->db->query($sql, array($deptno));
        $depts = $res->result();
        echo ‘第一種方法:使用?預留位置,參數作為數組<br>‘;
        foreach ($depts as $dept)
        {
            echo ‘編號:‘ . $dept->DEPTNO . ‘<br>‘;
            echo ‘名稱:‘ . $dept->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $dept->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }

        $sql = ‘select deptno,dname,loc from dept where deptno=‘ . $deptno;
        $res = $this->db->query($sql);
        $depts = $res->result();
        echo ‘第二種方法:構造字串<br>‘;
        foreach ($depts as $dept)
        {
            echo ‘編號:‘ . $dept->DEPTNO . ‘<br>‘;
            echo ‘名稱:‘ . $dept->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $dept->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

    public function addData($deptno, $deptname, $deptloc)
    {
        $sql = ‘insert into dept(deptno,dname,loc) values(?,?,?)‘;
        $bool = $this->db->query($sql, array($deptno, $deptname, $deptloc));
        if ($bool)
        {
            echo ‘插入成功!‘;
        }
    }

    public function delData($deptno)
    {
        $sql = ‘delete from dept where deptno=?‘;
        $bool = $this->db->query($sql, array($deptno));
        if ($bool)
        {
            echo ‘刪除資料成功!‘;
        }
    }

    public function updateData($detpno, $deptloc)
    {
        $sql = ‘update dept set loc=? where deptno =? ‘;
        $bool = $this->db->query($sql, array($deptloc, $detpno));
        if ($bool)
        {
            echo ‘更新資料成功!‘;
        }
    }

    public function arSelectData()
    {
        $this->load->model(‘MuseOra_AR‘);
        $rows = $this->MuseOra_AR->AR_selectData();
        foreach ($rows as $row)
        {
            echo ‘編號:‘ . $row->DEPTNO . ‘<br>‘;
            echo ‘名稱:‘ . $row->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $row->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

    public function arSelectDataWithParam($deptno)
    {
        $this->load->model(‘MuseOra_AR‘);
        $rows = $this->MuseOra_AR->AR_selectDataWithParam($deptno);
        foreach ($rows as $row)
        {
            echo ‘編號:‘ . $row->DEPTNO . ‘<br>‘;
            echo ‘名稱:‘ . $row->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $row->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

}

?>

 

 

--在model中的MuseOra_AR.php

<?php

class MuseOra_AR extends CI_Model
{

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

    function AR_selectData()
    {
        $this->db->order_by(‘deptno‘,‘ASC‘);//排序的方法
        $this->db->select(‘deptno,dname,loc‘);
        $res = $this->db->get(‘dept‘);
        return $res->result();
    }

    function AR_selectDataWithParam($deptno)
    {
        $this->db->where(‘deptno‘, $deptno);
        $this->db->select(‘deptno,dname,loc‘);
        $res = $this->db->get(‘dept‘);
        return $res->result();
    }

}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.