PHP connection mssql Some of the relevant experience and summary _php tutorial

Source: Internet
Author: User
In order to enable PHP connection MSSQL, the system needs to install mssql,php, and in the configuration of php.ini, Extension=php_mssql.dll Front; remove 1. Connect MSSQL $conn =mssql_connect (" Instance name or server IP "," User name "," password "); Test connection if ($conn) {echo "Connection succeeded";} 2. Select the database mssql_select_db ("dbname") to connect to; 3. Execute Query $rs = Mssql_query ("Select top 1 id,username from Tbname", $conn); Or directly execute the Update,insert statement, you can not assign a value to the return result Mssql_query ("Update tbname set username= ' NIUNV ' where id=1"); 4. Get the number of record set rows echo mssql_num_rows ($RS); 5. Get the recordset if ($row =mssql_fetch_array ($rs)) {$id = $row [0];//Gets the id field value $username = $row [1];//Gets the username field value} 6. Gets the ID of the newly added record ID field Set to the IDENTITY field, after the INSERT statement is executed, an @ @IDENTITY global variable value is generated, and the query is the ID of the last new record. Mssql_query ("INSERT into tbname (username) VALUES (' NV ')", $conn); $rs = Mssql_query ("SELECT @ @IDENTITY as ID", $conn); if ($row =mssql_fetch_array ($rs)) {echo $row [0];} 7. Release Recordset//More http://www.52mvc.com Mssql_free_result ($RS); 8. Close the connection mssql_close ($conn); Note: Using PHP to operate MSSQL than in the ASP connection MySQL to be simple, so, when the need for MSSQL and MySQL coexistence, using PHP to connect with MSSQL to operate MySQL and MSSQL is relatively simple and easy to use. If the ASP is connected to MySQL, Need to install a MySQL driver, default Windows ODBC is not installed, unfortunately ... 1. In WA client with at least MSSQL installed on the EB server 2. Open php.ini; Extension=php_mssql.dll the semicolon in front of the necessary words: need to develop EXTENSION_DIR 3. Recommended Use php<=4.0.9 <= 5.0.3 currently I have not connected successfully over 4.010 and 5.0.3 4. The connection page of the database can be obtained to the corresponding class on the phpe.net below is a class that I modified based there Server = $Server; $this->username = $UserName; $this->password = $PassWord; $this->database = $DataBase; }/** * Database connection **/function Db_connect () {$this->linkid = Mssql_pconnect ($this->server, $this->username, $this- >password); if (! $this->linkid) {$this->er = "Db_connect ($this->server, $this->username, $this->password) error"; return 0; } if (!mssql_select_db ($this->database, $this->linkid)) {$this->er = "mssql_select_db ($this->database,$ This->lastinsertid) Error "; return 0; } return $this->linkid; }/**public * Function:check the database, if exist then select * Exist:return 1 * not exist:return 0 */function Selec Tdatabase () {if (mssql_select_db ($this->database)) return 1; else return 0;}/** * Data operation **/function query ($STR) {if ($t His->linkid = = 0) {$this->er = "Database is not connected!! "; } $this->queryresult = Mssql_query ($STR); $this->queryresult = Mssql_query ($Str, $this->linkid); if (! $this->queryresult) {$this->er ="$Str. No operation succeeded, query error!! "; return 0;//**************** for PHP 4.3.9 or later error with 1} return $this->queryresult; }/** * Data gets **/function Fetch_array ($result) {if ($result! = "") $this->queryresult = $result; $rec =mssql_fetch_array ($this->queryresult); if (Is_array ($rec)) {return $rec;}//$this->er = "No data obtained!" "; 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); }/** * Gets the number of rows affected * Gets the number of rows that have been manipulated **/function num_rows ($result = "") {if ($result! = "") {$this->queryresult = $result; $row = Mssql_num_rows ($this->queryresult); return $row; }}/** * Get query Results---multiple **/function result_ar ($str = ') {if (empty ($STR)) {return 0;} $back = Array (); $this->queryresul t = $this->query ($STR); while ($row = $this->fetch_array ($this->queryresult)) {$back [] = $row;} return $back; }/** * Database information Paging * $Result database operations *str==sql statement *page = = Page *shownum = = Displays several pages */function page ($Str, $Page =0, $ShowNum =5) {$back = array ();//return data $maxNum = 0; if ($St r = = "") {$this->er = "no data"; return 0; } $this->queryresult = $this->query ($STR); if ($this->queryresult) {if ($Page = = "") {$nopa = 0;} else{$nopa = ($Page-1) * $ShowNum, if ($nopa <0) {$nopa = 0;}} $maxNum = $this->num_rows ($this->queryresult); $k = 0; $i = 0; $DD = $this->fetch_array ($this->queryresult); while ($dd && $nopa <= $maxNum && $i < $ShowNum) {if ($nopa >= $maxNum) $nopa = $maxNum; mssql_data_ Seek ($this->queryresult, $nopa); $row = $this->fetch_array ($this->queryresult); $nopa + +; $i + +; $back [] = $row; if ($nopa >= $maxNum) {break;}} } $this->pagenum = $maxNum; return $back; }/** * Page HTML page */function page_html ($DataNum =0, $Page =1, $ShowNum =3, $web, $Post = ") {if ($DataNum = = 0) {$back =" no number to query According to "; }else {if ($ShowNum <=0) {$ShowNum = 3;} if ($Page <=0) {$Page = 1;} if (Empty ($web)) {$web = "#";} $pAgenum = Ceil ($DataNum/$ShowNum); if ($Page <= 1) {$top = "home <<";} else {$top = "home <<";} if ($Page!==1) {$upPage = "prev";} else {$upPage = "previous";} if ($Page < $pageNum) {$downPage = "next page";} else {$downPage = "next page";} if ($Page = = $pageNum) {$foot = ">> Last";} else {$foot = ">> Last";} $back = <<

< span id= "url" itemprop= "url" >http://www.bkjia.com/phpjc/477777.html www.bkjia.com true http://www.bkjia.com/phpjc/477777.html techarticle to enable PHP to connect MSSQL, the system needs to install MSSQL, PHP, in the configuration of PHP.ini, Extension=php_mssql.dll the front; 1. Connect MSSQL $conn =mssql_connect (instance name or service ...

  • 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.