標籤:使用 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();
}
}
?>