php连接mysql与mssql 2005数据库代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-11 13:36:09 浏览: 评论:0
1,php连接mssql 2005 1,下载以下两个文件,放入php ext目录及system32.
php_sqlsrv_52_ts_vc6.dll(线程安全),php_sqlsrv_52_nts_vc6.dll(非线程安全).
vc6用于apache,vc9用于iis
2.修改php.ini
extension=php_sqlsrv_52_ts_vc6.dll
3.下载sqlncli.msi,微软官方可以下
安装的时候提示是sql server 2008的,不过2005也是可以用的.
4.测试代码
- <?php
- $servername = "127.0.0.1";
- $connectioninfo = array("database"=>"testdb","uid"=>"test","pwd"=>"test");
- $conn = sqlsrv_connect($servername, $connectioninfo);
- if($conn) {
- echo "connection established.<br>";
- } else {
- echo "connection could not be established.<br>";
- die(print_r(sqlsrv_errors(), true));
- exit();
- }
- $sql = "select * from t_employee";
- $result = sqlsrv_query($conn,$sql);
- $data = array();
- while($row=sqlsrv_fetch_array($result)) {
- $data[] = $row;
- }
- foreach($data as $p) {
- echo $p['name']."<br>";
- }
- sqlsrv_close($conn);
- echo "<br> done <br>";
- echo date("y-m-d h:i:s");
- ?>
mysql连接类:
- class db_handle {
- var $classname = "db_handle";
- var $server;
- var $username;
- var $password;
- var $database;
- var $linkid = 0;
- var $queryresult = "";
- var $lastinsertid = "";
- /* private ignore=>ignore the error and continue, halt=>report the error and halt, report=>report the error and continue */
- var $halt_on_error = "report";
- var $error = "";
- var $errno = 0;
- /**public
- * remark: this is the db_mysql_class's structure
- * function: set the server,username,password,database variable.
- */
- function db_handle($server = "", $username = "", $password = "", $database = "") {
- $this->server = $server;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- }
- /**public
- * function: connect database and select database
- * success: retun 1
- * failed: return 0
- */
- function connect() {
- $this->linkid = @mssql_pconnect ( $this->server, $this->username, $this->password );
- if (! $this->linkid) {
- $this->halt ( "mssql_pconnect($this->server,$this->username,$this->password): failed" );
- return 0;
- }
- if (! @mssql_select_db ( $this->database )) {
- $this->halt ( "mssql_select_db($this->database) failed." );
- return 0;
- }
- return 1;
- }
- /**public
- * function: check the database, if exist then select
- * exist: return 1
- * not exist: return 0
- */
- function selectdatabase() {
- if (@mssql_select_db ( $this->database ))
- return 1;
- else
- return 0;
- }
- /**public
- * function: execute sql instruction
- * success: return sql result.
- * failed: return 0;
- */
- function execquery($sql = "") {
- $this->connect();
- if ($this->linkid == 0) {
- $this->halt ( "execute sql failed: have not valid database connect." );
- return 0;
- }
- ob_start ();
- $this->queryresult = mssql_query ( $sql, $this->linkid );
- $error = ob_get_contents ();
- ob_end_clean ();
- if ($error) {
- $this->halt ( "execute sql: mssql_query($sql,$this->linkid) failed." );
- return 0;
- }
- $reg = "#insert into#";
- if (preg_match ( $reg, $sql )) {
- $sql = "select @@identity as id";
- $res = mssql_query ( $sql, $this->linkid );
- $this->lastinsertid = mssql_result ( $res, 0, id );
- }
- return $this->queryresult;
- }
- /**public
- * function: get the query result's row number
- * success: return the row fo the result
- * failed: return 0
- */
- function gettotalrownum($result = "") {
- if ($result != "")
- $this->queryresult = $result;
- $row = @mssql_num_rows ( $this->queryresult );
- if ($row >= 0)
- return $row;
- $this->halt ( "get a row of result failed: result $result is invalid." );
- return 0;
- }
- /**public
- * function: get the last insert record's id
- * success: return id
- * failed: return 0
- */
- function lastinsertid() {
- return $this->lastinsertid;
- }
- /**public
- * function: get a field's value
- * success: return value of the field
- * failed: return 0
- */
- function getfield($result = "", $row = 0, $field = 0) {
- if ($result != "")
- $this->queryresult = $result;
- $fieldvalue = @mssql_result ( $this->queryresult, $row, $field );
- if ($fieldvalue != "")
- return $fieldvalue;
- $this->halt ( "get field: mssql_result($this->queryresult,$row,$field) failed." );
- return 0;
- //here should have error handle
- }
- /**public
- * function: get next record
- * success: return a array of the record's value
- * failed: return 0
- */
- function nextrecord($result = "") {
- if ($result != "")
- $this->queryresult = $result;
- $record = @mssql_fetch_array ( $this->queryresult );
- if (is_array ( $record ))
- return $record;
- //$this->halt("get the next record failed: the result $result is invalid.");
- return 0;
- }
- /**public
- * function: free the query result
- * success return 1
- * failed: return 0
- */
- function freeresult($result = "") {
- if ($result != "")
- $this->queryresult = $result;
- return @mssql_free_result ( $this->queryresult );
- }
- /**public
- * function: set the halt_on_error's state
- * success: return 1
- * failed: return 0
- */
- function sethaltonerror($state = "ignore") {
- if (! ($state == "ignore" || $state == "report" || $state == "halt")) {
- $this->halt ( "set the halt_on_error fail: there is no state value $state" );
- return 0;
- }
- $this->halt_on_error = $state;
- return 1;
- }
- /**public
- * function: get the halt_on_error's state
- */
- function gethaltonerror() {
- return $this->halt_on_error;
- }
- /**public
- * function: get the class's name
- */
- function tostring() {
- return $this->classname;
- }
- /**private
- * function: error handle
- */
- function halt($msg) {
- $this->error = @mysql_error ( $this->linkid );
- $this->errno = @mysql_errno ( $this->linkid );
- if ($this->halt_on_error == "ignore")
- return;
- $this->makemsg ( $msg );
- if ($this->halt_on_error == "halt")
- die ( "session halted" );
- }//开源代码phpfensi.com
- /**private
- * function: make error information and print
- */
- function makemsg($msg) {
- printf ( "database error: %sn", $msg );
- printf ( "mysql error: %s (%s)n", $this->errno, $this->error );
- }
- }
Tags: php连接mysql php连接mssql
相关文章
- ·php连接mysql,mssql,acces,pdo等连接数据库代码(2014-08-18)
- ·一些简单的PHP连接数据库例子详解(2014-09-10)
- ·php 连接mysql实例代码(2014-09-11)
- ·php连接mysql数据库(2014-09-11)
- ·php连接mysql数据库并查询记录所有记录(2014-09-11)
- ·php连接mysql数据库(2014-09-11)
- ·php连接mysql,oracle,mssql数据库连接代码(2014-09-11)
- ·测试php连接mysql是否成功的代码分享(2020-08-31)
- ·PHP连接MySQL的2种方法小结以及防止乱码(2020-10-26)
- ·PHP以mysqli方式连接类完整代码实例(2021-03-21)
- ·Php连接及读取和写入mysql数据库的常用代码(2021-03-30)
- ·PHP连接和操作MySQL数据库基础教程(2021-04-15)
- ·PHP连接MySQL数据的操作要点(2021-05-17)
- ·PHP连接MYSQL数据库实例代码(2021-07-06)
- ·PHP连接MySQL数据库并以json格式输出(2021-09-17)
- ·php连接mysql数据库最简单的实现方法(2021-12-22)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)