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

在phpmyadmin里看到 " /> 老色鬼在线视频,欧美国产日产图区,亚洲国产一区二区三区高清

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

《PHP編程最快明白》第六講:Mysql數(shù)據(jù)庫操作

答案就是做成一個類--數(shù)據(jù)庫類就產生了。通過對函數(shù)的二次封裝,實現(xiàn)了非常好的重用。要用的時候再include進去。

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

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

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

寫sql語句時SUM()可以統(tǒng)計值;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'; //數(shù)據(jù)庫名稱
private $conn = ''; //數(shù)據(jù)庫鏈接資源
private $result = ''; //結果集
private $msg = ''; //返回結果
private $fields; //返回字段
private $fieldsNum = 0; //返回字段數(shù)
private $rowsNum = 0; //返回結果數(shù)
private $rowsRst = ''; //返回單條記錄的字段數(shù)組
private $filesArray = array(); //返回字段數(shù)組
private $rowsArray = array(); //返回結果數(shù)組
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();
}
//鏈接數(shù)據(jù)庫
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);
}

//取得查詢結果字段數(shù)目
function getFieldsNum($sql){
$this->mysql_query_rst($sql);
$this->fieldsNum = @mysql_num_fields($this->result);
}
//取得查詢結果行數(shù)目
function getRowsNum($sql){
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this->result);
}else{
return '';
}
}
//取得記錄數(shù)組有索引(單條記錄)
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 '';
}
}
//取得記錄數(shù)組有索引(多條記錄)全部
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 '';
}
}
//更新、刪除、添加記錄數(shù),返回影響到的行數(shù)
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 '';
}
}
//獲取對應的字段值,一條數(shù)字索引,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='';
}
//關閉數(shù)據(jù)庫
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位的密文,是沒有解密函數(shù)的,可以實現(xiàn)簡單的加密功能。
?>

php技術《PHP編程最快明白》第六講:Mysql數(shù)據(jù)庫操作,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 陆河县| 禄丰县| 灌南县| 六枝特区| 扎兰屯市| 木兰县| 军事| 泽普县| 云南省| 赞皇县| 新泰市| 嘉禾县| 调兵山市| 武川县| 广昌县| 连山| 商都县| 广东省| 隆回县| 镇宁| 苏尼特左旗| 明星| 东兰县| 芦山县| 射洪县| 府谷县| 安阳市| 密山市| 万州区| 二连浩特市| 阿拉善左旗| 普安县| 林甸县| 太湖县| 龙游县| 湖州市| 津市市| 天台县| 长汀县| 崇左市| 余庆县|