方法/:
為博愛等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及博愛網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站制作、博愛網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
1、數(shù)據(jù)庫連接第一步:配置mysql_connect()的參數(shù),參數(shù)依次為:主機地址,用戶名,用戶密碼;
2、mysql_pconnect()與mysql_connect()是不一樣的,pconnect顧名思義是持久連接;
3、服務(wù)器連接成功后,需要選擇需要用的數(shù)據(jù)庫;
4、使用mydql_close()可以關(guān)閉數(shù)據(jù)庫連接資源,避免長時間占用啟用資源消耗;
5、mysqli_connect( )是mysql連接的另一種方式,參數(shù)形式一樣;
6、首次使用mysql連接數(shù)據(jù)庫時,要記得使用輸入邏輯判斷,服務(wù)器連接不成功或者選擇數(shù)據(jù)庫不成功,都要用Mysql_error或者mysql_errno來報錯;
7、mysql的報錯,能夠幫助準(zhǔn)確地定位到錯誤發(fā)生在哪里。
試試我的代碼
?PHP
/*
創(chuàng)建ADO連接
*/
$conn = @new COM("ADODB.Connection") or die ("ADO Connection faild.");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("Database1.accdb");
$conn-Open($connstr);
/*
創(chuàng)建記錄集查詢
*/
$rs = @new COM("ADODB.RecordSet");
$rs-Open("select * from dbo_dirs",$conn,1,3);
/*
循環(huán)讀取數(shù)據(jù)
*/
while(!$rs-eof){
echo $rs-Fields["title"]-Value;
echo "br/";
$rs-Movenext(); //將記錄集指針下移
}
$rs-close();
?
以下為幾個php連接access數(shù)據(jù)庫和操作acess數(shù)據(jù)的方法,全部做成了類,應(yīng)用起來也更方便,也可摘用其中的部分代碼應(yīng)用。
?php
--------------------------------------------------------------------
//FileName:class.php
//Summary: Access數(shù)據(jù)庫操作類
// 使用范例:
//$databasepath="database.mdb";
//$dbusername="";
//$dbpassword="";
//include_once("class.php");
//$access=new Access($databasepath,$dbusername,$dbpassword);
--------------------------------------------------------------------
class Access
{
var $databasepath,$constr,$dbusername,$dbpassword,$link;
function Access($databasepath,$dbusername,$dbpassword)
{
$this-databasepath=$databasepath;
$this-username=$dbusername;
$this-password=$dbpassword;
$this-connect();
}
function connect()
{
$this-constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this-databasepath);
$this-link=odbc_connect($this-constr,$this-username,$this-password,SQL_CUR_USE_ODBC);
return $this-link;
//if($this-link) echo "恭喜你,數(shù)據(jù)庫連接成功!";
//else echo "數(shù)據(jù)庫連接失敗!";
}
function query($sql)
{
return @odbc_exec($this-link,$sql);
}
function first_array($sql)
{
return odbc_fetch_array($this-query($sql));
}
function fetch_row($query)
{
return odbc_fetch_row($query);
}
function total_num($sql)//取得記錄總數(shù)
{
return odbc_num_rows($this-query($sql));
}
function close()//關(guān)閉數(shù)據(jù)庫連接函數(shù)
{
odbc_close($this-link);
}
function insert($table,$field)//插入記錄函數(shù)
{
$temp=explode(',',$field);
$ins='';
for ($i=0;$icount($temp);$i++)
{
$ins.="'".$_POST[$temp[$i]]."',";
}
$ins=substr($ins,0,-1);
$sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
$this-query($sql);
}
function getinfo($table,$field,$id,$colnum)//取得當(dāng)條記錄詳細(xì)信息
{
$sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";
$query=$this-query($sql);
if($this-fetch_row($query))
{
for ($i=1;$i$colnum;$i++)
{
$info[$i]=odbc_result($query,$i);
}
}
return $info;
}
function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得記錄列表
{
$sql="SELECT * FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
$recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);
$i++;
}
return $recordlist;
}
function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得記錄列表
{
$sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
for ($j=0;$j$fieldnum;$j++)
{
$info[$j]=odbc_result($query,$j+1);
}
$rdlist[$i]=$info;
$i++;
}
return $rdlist;
}
function updateinfo($table,$field,$id,$set)//更新記錄
{
$sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleteinfo($table,$field,$id)//刪除記錄
{
$sql="DELETE FROM ".$table." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleterecord($table,$condition)//刪除指定條件的記錄
{
$sql="DELETE FROM ".$table." WHERE ".$condition;
$this-query($sql);
}
function getcondrecord($table,$condition="")// 取得指定條件的記錄數(shù)
{
$sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
$query=$this-query($sql);
$this-fetch_row($query);
$num=odbc_result($query,1);
return $num;
}
}
?
22222222
class.php文件:
[php]
?php
class Access//Access數(shù)據(jù)庫操作類
{
var $databasepath,$constr,$dbusername,$dbpassword,$link;//類的屬性
function Access($databasepath,$dbusername,$dbpassword)//構(gòu)造函數(shù)
{
$this-databasepath=$databasepath;
$this-username=$dbusername;
$this-password=$dbpassword;
$this-connect();
}
function connect()//數(shù)據(jù)庫連接函數(shù)
{
$this-constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this-databasepath);
$this-link=odbc_connect($this-constr,$this-username,$this-password,SQL_CUR_USE_ODBC);
return $this-link;
//if($this-link) echo "恭喜你,數(shù)據(jù)庫連接成功!";
//else echo "數(shù)據(jù)庫連接失敗!";
}
function query($sql)//送一個查詢字符串到數(shù)據(jù)庫中
{
return @odbc_exec($this-link,$sql);
}
function first_array($sql)//從access數(shù)據(jù)庫中返回一個數(shù)組
{
return @odbc_fetch_array($this-query($sql));
}
function fetch_row($query)//返回記錄中的一行
{
return odbc_fetch_row($query);
}
function total_num($sql)//取得記錄總數(shù)
{
return odbc_num_rows($this-query($sql));
}
function close()//關(guān)閉數(shù)據(jù)庫連接函數(shù)
{
odbc_close($this-link);
}
function insert($table,$field)//插入記錄函數(shù)
{
$temp=explode(',',$field);
$ins='';
for ($i=0;$i {
$ins.="'".$_POST[$temp[$i]]."',";
}
$ins=substr($ins,0,-1);
$sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
$this-query($sql);
}
function getinfo($table,$field,$id,$colnum)//取得當(dāng)條記錄詳細(xì)信息
{
$sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";
$query=$this-query($sql);
if($this-fetch_row($query))
{
for ($i=1;$i$colnum;$i++)
{
$info[$i]=odbc_result($query,$i);
}
}
return $info;
}
function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得記錄列表
{
$sql="SELECT * FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
$recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);
$i++;
}
return $recordlist;
}
function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得記錄列表
{
$sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
for ($j=0;$j$fieldnum;$j++)
{
$info[$j]=odbc_result($query,$j+1);
}
$rdlist[$i]=$info;
$i++;
}
return $rdlist;
}
function updateinfo($table,$field,$id,$set)//更新記錄函數(shù)
{
$sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleteinfo($table,$field,$id)//刪除記錄函數(shù)
{
$sql="DELETE FROM ".$table." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleterecord($table,$condition)//刪除指定條件的記錄函數(shù)
{
$sql="DELETE FROM ".$table." WHERE ".$condition;
$this-query($sql);
}
function getcondrecord($table,$condition="")//取得指定條件的記錄數(shù)函數(shù)
{
$sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
$query=$this-query($sql);
$this-fetch_row($query);
$num=odbc_result($query,1);
return $num;
}
}
?
[/php]
數(shù)據(jù)庫連接文件:
[php]
?php
$databasepath="data/database.mdb";//數(shù)據(jù)庫路徑
$dbusername="";//數(shù)據(jù)庫用戶名
$dbpassword="";//數(shù)據(jù)庫密碼
include_once("class.php");//調(diào)用數(shù)據(jù)庫操作類
$access=new Access($databasepath,$dbusername,$dbpassword);//新建一個數(shù)據(jù)庫操作類的對象
?
[/php]
[php]
?php
$sql="select * from $info where id=$id";
$result=$access-query($sql)or die("error2");
$array=odbc_fetch_array($result);
?
[/php]
333333333333
這個是為了 同時可以使用access和mysql而做的 先弄一個mysql的 然后又寫一個access的 所有的函數(shù)一一對應(yīng) 你可以看下 絕對原創(chuàng)喔~~
配置文件如下
$config['db']['type'] = "Mysql"; //數(shù)據(jù)庫類型“Mysql”,“Access”
$config['db']['database']= "ourcms"; //數(shù)據(jù)庫(文件)名
$config['db']['host'] = ""; //數(shù)據(jù)庫主機
$config['db']['username']= "7king"; //數(shù)據(jù)庫連接用戶名
$config['db']['password']= "tingting"; //數(shù)據(jù)庫連接密碼
/*
$config['db']['type'] = "Access"; //數(shù)據(jù)庫類型“Mysql”,“Access”
$config['db']['database']= "ourcms.mdb";//數(shù)據(jù)庫(文件)名
$config['db']['host'] = "";
$config['db']['username']= "";
$config['db']['password']= "";
?php
/**
* 2007.04 by zhaohe
*
* php連接access通用類
*
* 用法:
* 建立new Access類 = set_db設(shè)置數(shù)據(jù)路徑 = set_login 設(shè)置連接數(shù)據(jù)庫的用戶名和密碼
* = 通過set_conn 設(shè)置連接 =
* {
get_result 獲取查詢執(zhí)行結(jié)果; get_result_rows 獲取查詢執(zhí)行列表,一般是select
insert_info 插入新的記錄 update_info更新記錄
}
*
*
*/
class Access {
/**
* 類變量定義
* @param $conn mysql連接號
* @param $error 錯誤代號
* @param $username/$password 數(shù)據(jù)庫連接用戶名和密碼
* @param array $err_info 錯誤信息
*
* @param $debuginfo 調(diào)試信息
* @param $table 當(dāng)前操作數(shù)據(jù)表
*/
var $conn;
var $error;
var $database;
var $username = "";
var $password = "";
var $err_info = array(
0 = "沒有錯誤!",
1 = "數(shù)據(jù)庫連接失敗!",
2 = "sql執(zhí)行出錯!"
);
var $debuginfo="";
var $table;
/**
* 默認(rèn)構(gòu)造方法
**/
function Access( $arr=null ){
if( is_array($arr) ) {
$this-set_login( $arr['host'] , $arr['username'] , $arr['password'] );
$this-set_db( $arr['database'] );
$this-set_conn();
}
}
/**
* 設(shè)置數(shù)據(jù)庫文件名
* @param string $dbfile
*
* return void
*/
function set_db ( $dbfile ){
$this-database = $dbfile;
}
/**
* 設(shè)置連接數(shù)據(jù)庫的用戶名和密碼
* @param string $user 用戶名
* @param string $pwd 密碼
*
* @return void
*/
function set_login ( $user , $pwd ){
$this-username=$user;
$this-password=$pwd;
}
/**
* 創(chuàng)建數(shù)據(jù)庫連接
* @param
* return void
*/
function set_conn ( ){
if($this-conn=odbc_connect("DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath($this-database),$this-username,$this-password,SQL_CUR_USE_ODBC )) $this-error=0;
else $this-error=1;
}
/**
* 設(shè)置當(dāng)前操作的數(shù)據(jù)表
* @param string $tb
*
* @return void
*/
function set_table( $tb ) {
$this-table = $tb;
}
/**
* 返回sql查詢結(jié)果
* @param string $sql sql語句
*
* @return #id
*/
function get_result( $sql ){
return odbc_do( $this-conn , $sql );
}
/**
* 獲取查詢的結(jié)果
* @param string $sql
*
* @return array 結(jié)果的二維數(shù)組
*/
function get_result_rows( $sql ){
$array = array() ;
$result = $this-get_result( $sql );
while( $row = odbc_fetch_array( $result ) )
$array[] = $row ;
return $array;
}
/**
* 獲取部分查詢結(jié)果
*
* @param Array 數(shù)組
* @return Array
*/
function get_query_result( $cols , $tb=null , $order=null , $limit=null , $start=0 ) {
if( empty($tb) ) $tb=$this-table;
else $this-table=$tb;
if( is_array($cols) ) $col="[".implode('],[',$cols)."]";
else $col = $cols;
if( empty($limit) )
$sql = "select $col from $tb";
else
$sql ="select top $limit $col from $tb";;
if( isset($order) ) $sql.=" order by $order";
return $this-get_result_rows($sql);
}
/**
* 執(zhí)行數(shù)據(jù)庫插入操作
*
* @param $arr values列表,數(shù)組索引為數(shù)據(jù)表字段
* @param $tb 操作數(shù)據(jù)表 如果為空則為設(shè)置的當(dāng)前類的操作表
*/
function insert_info( $arr , $tb = "" ) {
$cols = array_keys( $arr );
$values = array_values( $arr );
if (empty($tb)) $tb = $this-tb;
/*
foreach( $arr as $key = $value ){
$cols[] = $key;
$values[] = $value;
}
*/
$sql = "insert into [$tb]([".implode("],[",$cols)."]) values('".implode("','",$values)."')";
//return $sql;
return $this-get_result( $sql );
}
/**
* 執(zhí)行數(shù)據(jù)庫更新操作
*
* @param array $arr 要更新的字段值 數(shù)組索引為表字段名
* @param array $con 條件數(shù)組
* @param string $tb 要操作的數(shù)據(jù)表
*
*/
function update_info( $arr , $con , $tb = "" ) {
$cols = array();
$conditions = array();
if (empty( $tb )) $tb = $this-tb;
foreach( $arr as $key = $value ){
$cols[] = "[$key]='$value'";
}
foreach( $con as $key = $value ) {
//檢查數(shù)據(jù)類型
if( is_int($value) || is_float($value) )
$conditions[] = "[$key]=$value";
else
$conditions[] = "[$key]='$value'";
}
$sql = "update [$tb] set ".implode(",",$cols)." where ".implode(" and ",$conditions);
//return $sql;
return $this-get_result( $sql );
}
}
?
mysql的類如下
class Mysql {
/**
* mysql連接執(zhí)行類,將sql的執(zhí)行實現(xiàn)數(shù)據(jù)庫無關(guān)性
*
*
*
*/
/**
* 類變量定義
* @param $conn mysql連接號
* @param $error 錯誤代號
* @param $username/$password 數(shù)據(jù)庫連接用戶名和密碼
* @param array $err_info 錯誤信息
*
* @param $debuginfo 調(diào)試信息
* @param $table 當(dāng)前操作數(shù)據(jù)表
*/
var $conn;
var $error;
var $username = "";
var $password = "";
var $host;
var $database;
var $err_info = array(
0 = "沒有錯誤!",
1 = "數(shù)據(jù)庫連接失敗!",
2 = "sql執(zhí)行出錯!"
);
var $debuginfo="";
var $table;
function Mysql( $arr=null ) {
if( is_array($arr) ) {//var_dump($arr);
$this-set_login( $arr['host'] , $arr['username'] , $arr['password'] );
$this-set_db( $arr['database'] );
$this-set_conn();
if( isset($this-error) $this-error!=0 ) die($this-err_info[$this-error]);
}
}
/**
* 設(shè)置數(shù)據(jù)庫名
* @param string $database
*
* return void
*/
function set_db ( $dbfile ){
$this-database = $dbfile;
}
/**
* 設(shè)置連接數(shù)據(jù)庫的用戶名和密碼
* @param string $user 用戶名
* @param string $pwd 密碼
*
* @return void
*/
function set_login ( $host , $user , $pwd ){
$this-host=$host;
$this-username=$user;
$this-password=$pwd;
}
/**
* 創(chuàng)建數(shù)據(jù)庫連接
* @param
* return void
*/
function set_conn (){
$this-conn=mysql_connect($this-host,$this-username,$this-password );
if ( isset($this-conn) mysql_select_db($this-database) )
$this-error=0;
else
$this-error=1;
}
/**
* 設(shè)置當(dāng)前操作的數(shù)據(jù)表
* @param string $tb
*
* @return void
*/
function set_table( $tb ) {
$this-table = $tb;
}
/**
* 返回sql查詢結(jié)果
* @param string $sql sql語句
*
* @return #id
*/
function get_result( $sql ){
return mysql_query( $sql , $this-conn );
}
/**
* 獲取查詢的結(jié)果
* @param string $sql
*
* @return array 結(jié)果的二維數(shù)組
*/
function get_result_rows( $sql ){
$array = array() ;
$result = $this-get_result( $sql );
while( $row = mysql_fetch_assoc( $result ) )
$array[] = $row ;
return $array;
}
/**
* 獲取部分查詢結(jié)果
*
* @param Array 數(shù)組
* @return Array
*/
function get_query_result( $cols , $tb=null , $condition , $order=null , $limit=null , $start=0 ) {
if( empty($tb) ) $tb=$this-table;
else $this-table=$tb;
if( is_array($cols) ) $col="`".implode('`,`',$cols)."`";
else $col = $cols;
if( isset($limit) )
$sql.="select top $limit $col from $tb";
else
$sql = "select $col from $tb";
if( isset($condition) ) $sql.=" where $condition";
if( isset($order) ) $sql.=" order by $order";
if( isset($limit) ) $sql.=" limit $start,$limit";
return $this-get_result_rows($sql);
}
/**
* 執(zhí)行數(shù)據(jù)庫插入操作
*
* @param $arr values列表,數(shù)組索引為數(shù)據(jù)表字段
* @param $tb 操作數(shù)據(jù)表 如果為空則為設(shè)置的當(dāng)前類的操作表
*/
function insert_info( $arr , $tb = "" ) {
$cols = array_keys( $arr );
$values = array_values( $arr );
if (empty($tb)) $tb = $this-table;
/*
foreach( $arr as $key = $value ){
$cols[] = $key;
$values[] = $value;
}
*/
$sql = "insert into [$tb](`".implode("`,`",$cols)."`) values('".implode("','",$values)."')";
//return $sql;
return $this-get_result( $sql );
}
/**
* 執(zhí)行數(shù)據(jù)庫更新操作
*
* @param array $arr 要更新的字段值 數(shù)組索引為表字段名
* @param array $con 條件數(shù)組
* @param string $tb 要操作的數(shù)據(jù)表
*
*/
function update_info( $arr , $con , $tb = "" ) {
$cols = array();
$conditions = array();
if (empty( $tb )) $tb = $this-table;
if( is_array($arr) ) {
foreach( $arr as $key = $value ){
$cols[] = "`$key`='$value'";
}
foreach( $con as $key = $value ) {
//檢查數(shù)據(jù)類型
if( is_int($value) || is_float($value) )
$conditions[] = "`$key`=$value";
else
$conditions[] = "`$key`='$value'";
}
$sql = "update `$tb` set ".implode(",",$cols)." where ".implode(" and ",$conditions);
}
else
$sql = "update `$tb` set $arr where $con";
//return $sql;
return $this-get_result( $sql );
}
}
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫;
檢查php環(huán)境是否已開啟mysql擴展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫主機地址
$host="localhost";
//定義mysql數(shù)據(jù)庫登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫
$conn = mysql_connect($host,$user,$pwd);
//對連接進(jìn)行判斷
if(!$conn){
die("數(shù)據(jù)庫連接失敗!".mysql_errno());
}else{
echo "數(shù)據(jù)庫連接成功!";
}
?
分享題目:phpmdb數(shù)據(jù)庫連接,php與數(shù)據(jù)庫連接
當(dāng)前地址:http://sd-ha.com/article26/hooejg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計公司、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計、品牌網(wǎng)站設(shè)計、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)