程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 《PHP編程最快明白》第六講:Mysql數據庫操作

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

編輯:PHP綜合
答案就是做成一個類--數據庫類就產生了。通過對函數的二次封裝,實現了非常好的重用。要用的時候再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位的密文,是沒有解密函數的,可以實現簡單的加密功能。
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved