php連mssql,access的方法 js連接access數據庫

 

一、php連mssql
$dbhost = '';
$dbuser = ''; //你的mssql用户名
$dbpass = ''; //你的mssql密码
$dbname = ''; //你的mssql库名

$connect=odbc_connect("Driver={SQL Server};Server=$dbhost;Database=$dbname","$dbuser","$dbpass");
$sql="select * from content";
$exec=odbc_exec($connect,$sql);
while($row = (odbc_fetch_array($exec)))
{
 $row['id']  //獲取字段值
 ...
}

 


二、php連access
$db=$_SERVER['DOCUMENT_ROOT']."/PHP_ACCESS/include/#mydb.mdb";   //這裏最好用$_SERVER['DOCUMENT_ROOT']獲取路徑
$conn = new COM('ADODB.Connection') or die('can not start Active X Data Objects');
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
     $rs = $conn->Execute('SELECT * FROM contents order by id desc');
     while(!$rs->EOF) 
            {
              echo $rs->Fields['name']->Value;
              $rs->MoveNext();
     }
   /*释放资源*/
   $rs->Close();
      $conn->Close();
      $rs = null;
      $conn = null;


Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Provider for ODBC Drivers<br/><b>Description:</b> [Microsoft][ODBC Microsoft Access Driver] Disk or network error.'
(*  xp上出現上述錯誤,但在2003上能正常運行,可能與電腦ODBC配置有關)???????

 

 

 

 

 

 

三、js連接access數據庫

 

scEngine.js文件

//仿数据库连接池类
function scDBPool(){
    try{
         this.con=new ActiveXObject("ADODB.Connection");
         this.con.Provider="Microsoft.Jet.OLEDB.4.0";
         this.rs=new ActiveXObject("ADODB.Recordset");
    }catch(e){
         this.con=null;
         this.rs=null;
    }
    this.filePath=null;
    this.dbPath=null;
};


//设置数据库文件相对(定位文件)路径和数据库名
scDBPool.prototype.setDB=function(dbPath){
    this.dbPath=dbPath;
};

//设置数据库定位文件,这一步可以进连接类中,这里写是方便使用任何名字的数据库
scDBPool.prototype.setDBPathPosition=function(urlFile){
    var filePath=location.href.substring(0, location.href.indexOf(urlFile));
    this.dbPath=(this.dbPath==null||this.dbPath=="") ? "/calendar.mdb" : this.dbPath;
    var path=filePath+this.dbPath;
   //去除path前面的"files://"字符串
    this.filePath=path.substring(8);
};

//同数据库建立连接
scDBPool.prototype.connect=function(){
 this.filePath="C:\\Documents and Settings\\zhen.wang\\Desktop\\js_calendar\\calendar.mdb";    //access路徑
    this.con.ConnectionString="Data Source="+this.filePath;
 //alert(this.con.ConnectionString);
    this.con.open;
};

//执行数据库语句返回结果集
scDBPool.prototype.executeQuery=function(sql){
    this.rs.open(sql,this.con);
};

//执行数据库语句不返回结果集
scDBPool.prototype.execute=function(sql){
    this.con.execute(sql);
};

//关闭结果集
scDBPool.prototype.rsClose=function(){
    this.rs.close();
    this.rs=null;
};

//关闭数据连接
scDBPool.prototype.conClose=function(){
    this.con.close();
    this.con=null;
};

 

 


調用:
<html>
<head><title>ddd</title>
<script language="javascript" src="scEngine.js"></script>
</head>
<body>

<script language="javascript">
var db=new scDBPool();
db.setDB("calendar.mdb");
db.connect();
var sql="insert into cTime(Week) values('ddd') ";
db.execute(sql);
/*
db.executeQuery(sql);
while(!db.rs.eof){
    var cnt = db.rs.Fields("Data");
    document.write(cnt);
    db.rs.moveNext;
}
*/
db.rsClose();
db.conClose();
</script>
</body>

 

 

 

 

 

 

 

 

 

 


 

你可能感兴趣的:(php連mssql,access的方法 js連接access數據庫)