CI框架下的PHP增刪改查總結(jié):
成都創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,公司以成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計(jì)等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶近千家,涉及國內(nèi)多個(gè)省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗(yàn)。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計(jì)、宣傳推廣等服務(wù)。 通過專業(yè)的設(shè)計(jì)、獨(dú)特的風(fēng)格,為不同客戶提供各種風(fēng)格的特色服務(wù)。
controllers下的 cquery.php文件
[php] view plain copy
?php
class CQuery extends Controller {
//構(gòu)造函數(shù)
function CQuery() {
parent::Controller();
// $this-load-database();
}
function index() {
//調(diào)用model 其中train為外層文件夾 MQuery為model名稱 queryList為重命名
$this-load-model('train/MQuery','queryList');
//獲得返回的結(jié)果集 這里確定調(diào)用model中的哪個(gè)方法
$result = $this-queryList-queryList();
//將結(jié)果集賦給res
$this-smarty-assign('res',$result);
//跳轉(zhuǎn)到顯示頁面
$this-smarty-view('train/vquery.tpl');
}
//進(jìn)入新增頁面
function addPage() {
$this-smarty-view('train/addPage.tpl');
}
//新增
function add() {
//獲得前臺(tái)數(shù)據(jù)
//用戶名
$memberName = $this-input-post('memberName');
//密碼
$password = $this-input-post('password');
//真實(shí)姓名
$userRealName = $this-input-post('userRealName');
//性別
$sex = $this-input-post('sex');
//出生日期
$bornDay = $this-input-post('bornDay');
//e_mail
$eMail = $this-input-post('eMail');
//密碼問題
$question = $this-input-post('question');
//密碼答案
$answer = $this-input-post('answer');
//調(diào)用model
$this-load-model('train/MQuery','addRecord');
//向model中的addRecord傳值
$result = $this-addRecord-addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判斷返回的結(jié)果,如果返回true,則調(diào)用本頁的index方法,不要寫 $result == false 因?yàn)榉祷氐闹滴幢厥莊alse 也有可能是""
if ($result) {
$this-index();
} else {
echo "add failed.";
}
}
//刪除
function deletePage() {
//獲得ID
$deleteID = $this-uri-segment(4);
//調(diào)用model
$this-load-model('train/MQuery','delRecord');
//將值傳入到model的delRecord方法中
$result = $this-delRecord-delRecord($deleteID);
//判斷返回值
if ($result) {
$this-index();
} else {
echo "delect failed.";
}
}
//修改先查詢
function changePage() {
$changeID = $this-uri-segment(4);
$this-load-model('train/MQuery','changeRecord');
$result = $this-changeRecord-changeRecord($changeID);
//將結(jié)果集賦給res
$this-smarty-assign('res',$result);
//跳轉(zhuǎn)到顯示頁面
$this-smarty-view('train/changePage.tpl');
}
//修改
function change() {
//獲得前臺(tái)數(shù)據(jù)
//ID
$ID = $this-input-post('id');
//用戶名
$memberName = $this-input-post('memberName');
//密碼
$password = $this-input-post('password');
//真實(shí)姓名
$userRealName = $this-input-post('userRealName');
//性別
$sex = $this-input-post('sex');
//出生日期
$bornDay = $this-input-post('bornDay');
//e_mail
$eMail = $this-input-post('eMail');
//密碼問題
$question = $this-input-post('question');
//密碼答案
$answer = $this-input-post('answer');
//調(diào)用model
$this-load-model('train/MQuery','change');
//向model中的change傳值
$result = $this-change-change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判斷返回的結(jié)果,如果返回true,則調(diào)用本頁的index方法,不要寫 $result == false 因?yàn)榉祷氐闹滴幢厥莊alse 也有可能是""
if ($result) {
$this-index();
} else {
echo "change failed.";
}
}
}
models中的 mquery.php 文件
[php] view plain copy
?php
class MQuery extends Model {
//構(gòu)造函數(shù)
function MQuery() {
parent::Model();
//連接數(shù)據(jù)庫
$this-load-database();
}
//查詢列表
function queryList() {
//防止select出的數(shù)據(jù)存在亂碼問題
//mysql_query("SET NAMES GBK");
//SQL語句
$sql = "SELECT ID,member_name,sex,e_mail FROM user_info_t";
//執(zhí)行SQL
$rs = $this-db-query($sql);
//將查詢結(jié)果放入到結(jié)果集中
$result = $rs-result();
//關(guān)閉數(shù)據(jù)庫
$this-db-close();
//將結(jié)果集返回
return $result;
}
//新增
function addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的數(shù)據(jù)存在亂碼問題
//mysql_query("SET NAMES GBK");
//SQL語句
$sql = "INSERT INTO user_info_t (member_name,password,user_real_name,sex,born_day,e_mail,question,answer) " .
"VALUES ('$memberName','$password','$userRealName','$sex','$bornDay','$eMail','$question','$answer')";
//執(zhí)行SQL
$result = $this-db-query($sql);
//關(guān)閉數(shù)據(jù)庫
$this-db-close();
//返回值
return $result;
}
//刪除
function delRecord($deleteID) {
//防止select出的數(shù)據(jù)存在亂碼問題
//mysql_query("SET NAMES GBK");
$sql = "DELETE FROM user_info_t WHERE ID = $deleteID";
$result = $this-db-query($sql);
$this-db-close();
return $result;
}
//修改前查詢
function changeRecord($changeID) {
//防止select出的數(shù)據(jù)存在亂碼問題
//mysql_query("SET NAMES GBK");
$sql = "SELECT ID,member_name,password,user_real_name,sex,born_day,e_mail,question,answer FROM user_info_t WHERE ID = $changeID";
//執(zhí)行SQL
$rs = $this-db-query($sql);
$result = $rs-row();//$result = $rs[0]
//關(guān)閉數(shù)據(jù)庫
$this-db-close();
//將結(jié)果集返回
return $result;
}
//修改
function change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的數(shù)據(jù)存在亂碼問題
//mysql_query("SET NAMES GBK");
//SQL語句
$sql = "update user_info_t set member_name = '$memberName',password = '$password', user_real_name = '$userRealName'," .
"sex = '$sex',born_day = '$bornDay',e_mail = '$eMail',question = '$question',answer = '$answer'" .
"where ID = $ID";
//執(zhí)行SQL
$result = $this-db-query($sql);
//關(guān)閉數(shù)據(jù)庫
$this-db-close();
//返回值
return $result;
}
}
views 下的 addPage.tpl文件
[php] view plain copy
html
head
/head
bodyform action="{{site_url url='train/cquery/add'}}" method="post"
table border='1'
tr
td用戶名/td
tdinput type="text" class="text" name="memberName" id="memberName"http://td
/tr
tr
td密碼/td
tdinput type="text" class="text" name="password" id="password"http://td
/tr
tr
td真實(shí)姓名/td
tdinput type="text" class="text" name="userRealName" id="userRealName"http://td
/tr
tr
td性別/td
tdinput type="text" class="text" name="sex" id="sex"http://td
/tr
tr
td出生日期/td
tdinput type="text" class="text" name="bornDay" id="bornDay"http://td
/tr
tr
tde_mail/td
tdinput type="text" class="text" name="eMail" id="eMail"http://td
/tr
tr
td密碼問題/td
tdinput type="text" class="text" name="question" id="question"http://td
/tr
tr
td密碼答案/td
tdinput type="text" class="text" name="answer" id="answer"http://td
/tr
/table
table
tr
tdinput type="submit" class="button" name="OK" value="提交" /
/td
/tr
/table/form
/body
/html
你在查詢數(shù)據(jù)庫時(shí)沒有給$this-db-where('uname',$id);這里的ID值,所以會(huì)報(bào)錯(cuò),至于輸出的注冊成功是因?yàn)閕f ($user[0]-uid =='')是成立的所以會(huì)有輸出,把$id值是否為空做下判斷,如果為空則不查詢。
function user_select($id)
{
if($id=='') return false;
$this-db-where('uname',$id);
$this-db-select('*');
$query=$this-db-get('user');
return $query-result();
}
1、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之 mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
從result_set 的指定row 中獲取一個(gè)field 的數(shù)據(jù). 簡單但是效率低.
舉例:
$link1 = @mysql_connect("server1",
"webuser", "password")
or die("Could not connect
to mysql server!");
@mysql_select_db("company")
or die("Could not select database!");
$query = "select id, name
from product order by name";
$result = mysql_query($query);
$id = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
mysql_close();
注意,上述代碼只是輸出結(jié)果集中的第一條數(shù)據(jù)的字段值,如果要輸出所有記錄,需要循環(huán)處理.
for ($i = 0; $i = mysql_num_rows($result); $i++)
{
$id = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
echo "Product: $name ($id)";
}
注意,如果查詢字段名是別名,則mysql_result中就使用別名.
2、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_row()
array mysql_fetch_row(resource result_set)
從result_set中獲取整行,把數(shù)據(jù)放入數(shù)組中.
舉例(注意和list 的巧妙配合):
$query = "select id,
name from product order by name";
$result = mysql_query($query);
while(list($id, $name)
= mysql_fetch_row($result)) {
echo "Product: $name ($id)";
}
3、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_array()
array mysql_fetch_array(resource result_set [,int result_type])
mysql_fetch_row()的增強(qiáng)版.
將result_set的每一行獲取為一個(gè)關(guān)聯(lián)數(shù)組或/和數(shù)值索引數(shù)組.
默認(rèn)獲取兩種數(shù)組,result_type可以設(shè)置:
MYSQL_ASSOC:返回關(guān)聯(lián)數(shù)組,字段名=字段值
MYSQL_NUM:返回?cái)?shù)值索引數(shù)組.
MYSQL_BOTH:獲取兩種數(shù)組.因此每個(gè)字段可以按索引偏移引用,也可以按字段名引用.
舉例:
$query = "select id,
name from product order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array
($result, MYSQL_BOTH)) {
$name = $row['name'];
//或者 $name = $row[1];
$name = $row['id'];
//或者 $name = $row[0];
echo "Product: $name ($id)";
}
4、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_assoc()
array mysql_fetch_assoc(resource result_set)
相當(dāng)于 mysql_fetch_array($result, MYSQL_ASSOC)
5、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_object()
object mysql_fetch_object(resource result_set)
和mysql_fetch_array()功能一樣,不過返回的不是數(shù)組,而是一個(gè)對象.
舉例:
$query = "select id, name
from product order by name";
$result = mysql_query($query);
while($row = mysql_fetch_object
($result)) {
$name = $row-name;
$name = $row-id;
echo "Product: $name ($id)";
}
以上這些函數(shù)就是PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)的全部總結(jié)。