国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

《PHP編程最快明白》第六講:Mysql數據庫操作

2024-05-04 23:47:20
字體:
來源:轉載
供稿:網友
答案就是做成一個類--數據庫類就產生了。通過對函數的二次封裝,實現了非常好的重用。要用的時候再include進去。

在講PHP數據庫之前,先介紹一下Mysql要點:大家可以用phpmyadmin學習數據庫操作。

在phpmyadmin里看到編碼這一項全部選中文utf-8就對了。

Mysql數據庫類型主要是: char(固定空間字符串,多大就是多少個中文字符)、varchar(可變空間字符串,多大就是初始化多少個中文字符)、int(整數多大就是多少位)、float(浮點數)、timestamp(日期,可選建立時自動創建,輸出時就已經是格式化過的date)、text(文本)、bool(布爾型)

寫sql語句時SUM()可以統計值;order by 'id' DESC LIMIT 10,10等要活用。

在phpmyadmin學一下sql語句增刪改查就行了。

實例20 Mysql類
復制代碼 代碼如下:

<?php
class opmysql{
private $host = 'localhost'; //服務器地址
private $name = 'root'; //登錄賬號
private $pwd = ''; //登錄密碼
private $dBase = 'a0606123620'; //數據庫名稱
private $conn = ''; //數據庫鏈接資源
private $result = ''; //結果集
private $msg = ''; //返回結果
private $fields; //返回字段
private $fieldsNum = 0; //返回字段數
private $rowsNum = 0; //返回結果數
private $rowsRst = ''; //返回單條記錄的字段數組
private $filesArray = array(); //返回字段數組
private $rowsArray = array(); //返回結果數組
private $idusername=array();
private $idsubtitle=array();
//初始化類
function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '')
$this->host = $host;
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($dBase != '')
$this->dBase = $dBase;
$this->init_conn();
}
//鏈接數據庫
function init_conn(){
$this->conn=@mysql_connect($this->host,$this->name,$this->pwd);
@mysql_select_db($this->dBase,$this->conn);
mysql_query("set names utf8");
}
//查詢結果
function mysql_query_rst($sql){
if($this->conn == ''){
$this->init_conn();
}
$this->result = @mysql_query($sql,$this->conn);
}

//取得查詢結果字段數目
function getFieldsNum($sql){
$this->mysql_query_rst($sql);
$this->fieldsNum = @mysql_num_fields($this->result);
}
//取得查詢結果行數目
function getRowsNum($sql){
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this->result);
}else{
return '';
}
}
//取得記錄數組有索引(單條記錄)
function getRowsRst($sql){
$this->mysql_query_rst($sql);
if(mysql_error() == 0){
$this->rowsRst = mysql_fetch_array($this->result,MYSQL_ASSOC);
return $this->rowsRst;
}else{
return '';
}
}
//取得記錄數組有索引(多條記錄)全部
function getRowsArray($sql){
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this->result,MYSQL_ASSOC)) {
$this->rowsArray[] = $row;
}
return $this->rowsArray;
}else{
return '';
}
}
//更新、刪除、添加記錄數,返回影響到的行數
function uidRst($sql){
if($this->conn == ''){
$this->init_conn();
}
@mysql_query($sql);
$this->rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this->rowsNum;
}else{
return '';
}
}
//獲取對應的字段值,一條數字索引,mysql_array_rows才是帶字段索引
function getFields($sql,$fields){
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this->result) > 0){
$tmpfld = @mysql_fetch_row($this->result);
$this->fields = $tmpfld[$fields];

}
return $this->fields;
}else{
return '';
}
}

//錯誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this->msg = mysql_error();
}
return $this->msg;
}
//釋放結果集
function close_rst(){
mysql_free_result($this->result);
$this->msg = '';
$this->fieldsNum = 0;
$this->rowsNum = 0;
$this->filesArray = '';
$this->rowsArray = '';
$this->idsubtitle='';
$this->idusername='';
}
//關閉數據庫
function close_conn(){
$this->close_rst();
mysql_close($this->conn);
$this->conn = '';
}
}
?>

實例21 類的使用、密碼的md5加密
復制代碼 代碼如下:

<?php
$conne = new opmysql();
$conne-> getRowsArray($sql);
$conne-> close_conn();
$password=”123456一二三四五六”;
echo md5($password.”www.kuphp.com”);//輸出為32位的密文,是沒有解密函數的,可以實現簡單的加密功能。
?>
 


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 剑河县| 长武县| 德清县| 嵊州市| 阿图什市| 新沂市| 阿坝| 云霄县| 都匀市| 土默特右旗| 小金县| 呼图壁县| 安远县| 苏尼特左旗| 墨竹工卡县| 五莲县| 汶川县| 当涂县| 洮南市| 报价| 龙门县| 凤冈县| 滨州市| 富顺县| 锡林浩特市| 大余县| 郯城县| 威海市| 新沂市| 广平县| 昌江| 车险| 南丹县| 汉川市| 黄梅县| 任丘市| 额济纳旗| 菏泽市| 天门市| 太湖县| 文化|