我也剛剛學(xué)PHP,正在研究中,雖然你只給10分........
創(chuàng)新互聯(lián)成立與2013年,我們提供高端網(wǎng)站建設(shè)、小程序制作、電商視覺設(shè)計(jì)、成都APP應(yīng)用開發(fā)及網(wǎng)絡(luò)營(yíng)銷搜索優(yōu)化服務(wù),在傳統(tǒng)互聯(lián)網(wǎng)與移動(dòng)互聯(lián)網(wǎng)發(fā)展的背景下,我們堅(jiān)守著用標(biāo)準(zhǔn)的設(shè)計(jì)方案與技術(shù)開發(fā)實(shí)力作基礎(chǔ),以企業(yè)及品牌的互聯(lián)網(wǎng)商業(yè)目標(biāo)為核心,為客戶打造具商業(yè)價(jià)值與用戶體驗(yàn)的互聯(lián)網(wǎng)+產(chǎn)品。
首先,將代碼保存到一個(gè)文件,如:mysql.class.php
其次,在一個(gè)常用的文件里調(diào)用:比如頭部文件header.php,因?yàn)槲曳旁诟夸浰杂孟旅娣绞綄?dǎo)入其他文件:
require dirname(__FILE__) . 'include/config.php';
//導(dǎo)入類文件
require dirname(__FILE__) . 'include/mysql.class.php';
//定義一個(gè)類及初始化數(shù)據(jù)庫(kù)類
$db = new mysql($db_host, $db_user, $db_pass, $db_name);
$db_host = $db_user = $db_pass = $db_name = NULL;
然后,在test.php文件調(diào)用:
require_once dirname(__FILE__) . '/header.php';
使用方法:
$sql = "讀取表";
$res = $db-query($sql);
$info = array();//定義數(shù)組
while($row=$db-fetchRow($res))
{
$arr['id'] = $row['id'];
$arr['title'] = $row['title'];
$info[] = $arr;
}
可以在顯示的地方用:
foreach($info as $i)
{
echo $i['title']."br /";
}
或是直接使用while
還用另一種調(diào)用方式:
$here_area = $db-getRow("select areaid,areaname from {$table}area where areaid='$areaid'");
$here[] = array('name'=$here_area['areaname'],'id'=$here_area['areaid']);
測(cè)試通過,因?yàn)槲艺谑褂?....................................
config.php代碼:
?php
$db_host = "localhost";
$db_name = "test";
$db_user = "root";
$db_pass = "";
$table = "mini_";
$charset = "gb2312";
$dbcharset = "gbk";
?
mysql.class.php代碼:
?php
class mysql
{
var $link = NULL;
//自動(dòng)執(zhí)行__construct php5類構(gòu)建方法,如果PHP4和PHP5同時(shí)使用會(huì)自動(dòng)使用PHP5的方法
function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
//自動(dòng)執(zhí)行時(shí)調(diào)用mysql函數(shù)
$this-mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
//php4類構(gòu)建方法,如果沒有 __construct 就自動(dòng)執(zhí)行此功能
function mysql($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
if ($quiet)
{
$this-connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
else
{
$this-settings = array(
'dbhost' = $dbhost,
'dbuser' = $dbuser,
'dbpw' = $dbpw,
'dbname' = $dbname,
'charset' = $charset,
'pconnect' = $pconnect
);
}
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
global $dbcharset;
if ($pconnect)
{
if (!($this-link = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this-ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
return false;
}
}
else
{
if (PHP_VERSION = '4.2')
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw, true);
}
else
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000);
}
if (!$this-link)
{
if (!$quiet)
{
$this-ErrorMsg("Can't Connect MySQL Server($dbhost)!");
}
return false;
}
}
$this-dbhash = md5($this-root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this-version = mysql_get_server_info($this-link);
if ($this-version '4.1')
{
if ($dbcharset != 'latin1')
{
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this-link);
}
if ($this-version '5.0.1')
{
mysql_query("SET sql_mode=''", $this-link);
}
}
if ($dbname)
{
if (mysql_select_db($dbname, $this-link) === false )
{
if (!$quiet)
{
$this-ErrorMsg("Can't select MySQL database($dbname)!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function query($sql, $type = '')
{
if ($this-link === NULL)
{
$this-connect($this-settings['dbhost'], $this-settings['dbuser'], $this-settings['dbpw'], $this-settings['dbname'], $this-settings['charset'], $this-settings['pconnect']);
$this-settings = array();
}
if ($this-queryCount++ = 99)
{
$this-queryLog[] = $sql;
}
if ($this-queryTime == '')
{
if (PHP_VERSION = '5.0.0')
{
$this-queryTime = microtime(true);
}
else
{
$this-queryTime = microtime();
}
}
if (!($query = mysql_query($sql, $this-link)) $type != 'SILENT')
{
$this-error_message[]['message'] = 'MySQL Query Error';
$this-error_message[]['sql'] = $sql;
$this-error_message[]['error'] = mysql_error($this-link);
$this-error_message[]['errno'] = mysql_errno($this-link);
$this-ErrorMsg();
return false;
}
return $query;
}
function affected_rows()
{
return mysql_affected_rows($this-link);
}
function num_fields($query)
{
return mysql_num_fields($query);
}
function error()
{
return mysql_error($this-link);
}
function errno()
{
return mysql_errno($this-link);
}
function num_rows($query)
{
return mysql_num_rows($query);
}
function insert_id()
{
return mysql_insert_id($this-link);
}
function fetchRow($query)
{
return mysql_fetch_assoc($query);
}
function fetcharray($query)
{
return mysql_fetch_array($query);
}
function version()
{
return $this-version;
}
function close()
{
return mysql_close($this-link);
}
function ErrorMsg($message = '', $sql = '')
{
if ($message)
{
echo "$message\n\n";
}
else
{
echo "bMySQL server error report:";
print_r($this-error_message);
}
exit;
}
function getCol($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_row($res))
{
$arr[] = $row[0];
}
return $arr;
}
else
{
return false;
}
}
function getOne($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
$row = mysql_fetch_row($res);
if ($row !== false)
{
return $row[0];
}
else
{
return '';
}
}
else
{
return false;
}
}
function getAll($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
else
{
return false;
}
}
//使用: getRow($sql,true) 如果有true那值是 limit 1,讀取一條信息
function getRow($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
return mysql_fetch_assoc($res);
}
else
{
return false;
}
}
}
?
?php
class MySQL{
private $host; //服務(wù)器地址
private $name; //登錄賬號(hào)
private $pwd; //登錄密碼
private $dBase; //數(shù)據(jù)庫(kù)名稱
private $conn; //數(shù)據(jù)庫(kù)鏈接資源
private $result; //結(jié)果集
private $msg; //返回結(jié)果
private $fields; //返回字段
private $fieldsNum; //返回字段數(shù)
private $rowsNum; //返回結(jié)果數(shù)
private $rowsRst; //返回單條記錄的字段數(shù)組
private $filesArray = array(); //返回字段數(shù)組
private $rowsArray = array(); //返回結(jié)果數(shù)組
private $charset='utf8'; //設(shè)置操作的字符集
private $query_count=0; //查詢結(jié)果次數(shù)
static private $_instance; //存儲(chǔ)對(duì)象
//初始化類
private 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();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this-$name=$value;
}
public function __get($name){
return $this-$name;
}
//鏈接數(shù)據(jù)庫(kù)
function init_conn(){
$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die('connect db fail !');
@mysql_select_db($this-dBase,$this-conn) or die('select db fail !');
mysql_query("set names ".$this-charset);
}
//查詢結(jié)果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數(shù)
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結(jié)果數(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,$type=MYSQL_BOTH){
$this-mysql_query_rst($sql);
if(empty($this-result)) return '';
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
}else{
return '';
}
}
//取得記錄數(shù)組(多條記錄)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : '';
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
}
return $this-rowsArray;
}else{
return '';
}
}
//更新、刪除、添加記錄數(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ù)據(jù)庫(kù)的id值
function returnRstId($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//獲取對(duì)應(yīng)的字段值
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 '';
}
}
//錯(cuò)誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結(jié)果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
}
//關(guān)閉數(shù)據(jù)庫(kù)
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
}
//取得數(shù)據(jù)庫(kù)版本
function db_version() {
return mysql_get_server_info();
}
}
ezSQL是一個(gè)非常好用的PHP數(shù)據(jù)庫(kù)操作類 著名的開源博客WordPress的數(shù)據(jù)庫(kù)操作就使用了ezSQL的MySQL部分 該數(shù)據(jù)庫(kù)操作類支持幾乎所有主流的數(shù)據(jù)庫(kù) 如 PHP PDO mySQL Oracle InterBase/FireBird PostgreSQL SQLite以及MS SQL等 ezSQL具有很強(qiáng)的調(diào)試功能 可以快速地查看SQL代碼的執(zhí)行情況 使用ezSQL 可以為我們節(jié)省開發(fā)時(shí)間 簡(jiǎn)化代碼并提高運(yùn)行效率
ezSQL的優(yōu)點(diǎn)就不用多說了 它小巧 快速 簡(jiǎn)單 易用 并且開源 還有就是安全 你沒想到的細(xì)節(jié)它都為你考慮了 你只需要在你的腳本開頭包含相關(guān)的PHP文件 然后你就可以使用更好用的一套ezSQL函數(shù)來(lái)代替標(biāo)準(zhǔn)的PHP數(shù)據(jù)庫(kù)操作函數(shù)
下面是ezSQL中一些主要的函數(shù)
$db get_results 從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù)集
$db get_row 從數(shù)據(jù)庫(kù)中讀取一行數(shù)據(jù)
$db get_col 從數(shù)據(jù)庫(kù)中讀取一列指定的數(shù)據(jù)集
$db get_var 從數(shù)據(jù)庫(kù)的數(shù)據(jù)集中讀取一個(gè)值
$db query 執(zhí)行一條SQL語(yǔ)句
$db debug 打印最后執(zhí)行的SQL語(yǔ)句及其返回的結(jié)果
$db vardump 打印變量的結(jié)構(gòu)及其內(nèi)容
$db select 選擇一個(gè)新數(shù)據(jù)庫(kù)
$db get_col_info 獲取列的信息
$db hide_errors 隱藏錯(cuò)誤
$db show_errors 顯示錯(cuò)誤
ezSQL的使用方法很簡(jiǎn)單 首先下載ezSQL源代碼 然后將ez_sql_core php文件和ez_sql_mysql php文件(這里以mySQL為例)放到與你的腳本文件相同的目錄下 然后將下面的代碼添加到你的腳本文件的最前面 這樣就可以正常使用ezSQL了
?php// 包含ezSQL的核心文件include_once?"ez_sql_core php";// 包含ezSQL具體的數(shù)據(jù)庫(kù)文件 這里以mySQL為例include_once?"ez_sql_mysql php";// 初始化數(shù)據(jù)庫(kù)對(duì)象并建立數(shù)據(jù)庫(kù)連接$db?=?new?ezSQL_mysql( db_user db_password db_name db_host );?
下面是ezSQL中一些主要函數(shù)的應(yīng)用實(shí)例 這些代碼均來(lái)自于ezSQL的官方幫助文檔
實(shí)例一
// Select multiple records from the database and print them out $users?=?$db get_results("SELECT name email FROM users");foreach?(?$users?as?$user?)?{? ? ? ? ? ??// Access data using object syntax? ? ? ? ? ??echo?$user name;? ? ? ? ? ??echo?$user email;}
實(shí)例二
// Get one row from the database and print it out $user?=?$db get_row("SELECT name email FROM users WHERE id = ");echo?$user name;echo?$user email;
實(shí)例三
// Get one variable from the database and print it out $var?=?$db get_var("SELECT count(*) FROM users");echo?$var;
實(shí)例四
// Insert into the database$db query("INSERT INTO users (id name email) VALUES (NULL justin jv@foo )");
實(shí)例五
// Update the database$db query("UPDATE users SET name = Justin WHERE id = )");
實(shí)例六
// Display last query and all associated results$db debug();
實(shí)例七
// Display the structure and contents of any result(s) or any variable$results?=?$db get_results("SELECT name email FROM users");$db vardump($results);
實(shí)例八
// Get one column (based on column index) and print it out $names?=?$db get_col("SELECT name email FROM users" )foreach?(?$names?as?$name?)?{? ??echo?$name;}
實(shí)例九
// Same as above ‘but quicker’foreach?(?$db get_col("SELECT name email FROM users" )?as?$name?)?{? ??echo?$name;}
實(shí)例十
lishixinzhi/Article/program/PHP/201311/21297
文章題目:數(shù)據(jù)庫(kù)類php 數(shù)據(jù)庫(kù)類型的劃分依據(jù)是
URL地址:http://www.rwnh.cn/article0/ddgoeio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、做網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、企業(yè)建站、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)