PHP connect MySQL with MSSQL 2005 database Code _php Tutorial

Source: Internet
Author: User
Tags echo date mysql tutorial
PHP Tutorial Connect MSSQL 2005 1. Download the following two files into the PHP ext directory and System32

Php_sqlsrv_52_ts_vc6.dll (thread safe)

Php_sqlsrv_52_nts_vc6.dll (non-thread safe)

VC6 for APACHE,VC9 for IIS

2. Modify PHP.ini

Extension=php_sqlsrv_52_ts_vc6.dll

3. Download Sqlncli.msi, Microsoft Official can under

The prompt for installation is SQL Server 2008, but 2005 is also available.

4. Test code

$servername = "127.0.0.1";
$connectioninfo = Array ("Database" = "TestDB", "uid" = "test", "pwd" = "test");
$conn = Sqlsrv_connect ($servername, $connectioninfo);

if ($conn) {
echo "Connection established.
";
} else {
echo "Connection could not being established.
";
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 ']. "
";
}

Sqlsrv_close ($conn);

echo "
Done
";
echo Date ("y-m-d h:i:s");

?>


MySQL Tutorial Connection class

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 C Ontinue * *
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 has the 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");
}

/**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);
}
}

http://www.bkjia.com/PHPjc/630765.html www.bkjia.com true http://www.bkjia.com/PHPjc/630765.html techarticle PHP Tutorial Connect MSSQL 2005 1. Download the following two files into the PHP ext directory and System32 php_sqlsrv_52_ts_vc6.dll (thread safe) Php_sqlsrv_52_nts_vc6.dll ( Non-thread safe) VC6 for APA ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.