Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect MSSQL in PHP

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to connect to MSSQL in PHP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The implementation method of connecting PHP to MSSQL. Ms SQL refers to Microsoft's SQLServer database server, which is a database platform that provides a complete solution of the database from the server to the terminal. The database server part is a database management system used to establish, use and maintain the database. At the beginning, SQL Server was not a product developed by Microsoft itself, but it was produced in cooperation with Sybase in order to compete with IBM at that time. Its earliest developer was Sybase. At the same time, Microsoft also cooperated with Sybase in the research and development of SQL Server 4.2. Microsoft also transplanted SQL Server 4.2 to Windows NT (then version 3.1). After terminating the partnership with Sybase, it independently developed SQL Server version 6.0. Later SQL Server was developed by Microsoft itself.

The realization method of connecting MSSQL with PHP

In order for PHP to connect to MSSQL, the system needs to install MSSQL,PHP, and in the configuration in PHP.ini, remove the front of; extension=php_mssql.dll

1. Connect MSSQL

$conn=mssql_connect ("instance name or server IP", "user name", "password")

/ / Test the connection

If ($conn)

{

Echo "connected successfully"

}

two。 Select the database to connect to

1mssql_select_db ("dbname")

3. Execute query

$rs = mssql_query ("select top 1 id,username from tbname", $conn)

/ / or execute statements such as update,insert directly without assigning a value to the returned result

Mssql_query ("update tbname set username='niunv' where id=1")

4. Get the number of recordset rows

1echo mssql_num_rows ($rs)

5. Get recordset

If ($row=mssql_fetch_array ($rs))

{

$id = $row [0]; / / get the ID field value

$username = $row [1]; / / get the username field value

}

6. Get the ID of the newly added record

Set the id field to the IDENTITY field, and after executing the insert statement, a @ @ IDENTITY global variable value will be generated, and the query will be 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 the recordset

1mssql_free_result ($rs)

8. Close the connection

1mssql_close ($conn)

Note: it is easier to operate MSSQL with PHP than to connect MYSQL in ASP, so when you need to co-exist with MSSQL and MYSQL, it is easier to use PHP to connect MSSQL to operate the coexistence of MYSQL and MSSQL. If ASP is connected to MYSQL, you need to install a MYSQL driver. The default ODBC for windows is not installed. It is a pity.

At least mssql client is installed on the web server

Open php.ini to remove the semicolon before; extension=php_mssql.dll

If necessary: need to develop extension_dir

PhpuserName = $UserName is recommended

$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 selectDatabase () {

If (mssql_select_db ($this- > dataBase))

Return 1

Else

Return 0

}

/ * *

* data manipulation

* * /

Function query ($Str) {

If ($this- > linkID = = 0) {

$this- > ER = "the database is not connected yet!"

}

$this- > queryResult = mssql_query ($Str)

/ / $this- > queryResult = mssql_query ($Str,$this- > linkID)

If (! $this- > queryResult) {

$this- > ER = "$Str. Failed operation, query errorless!"

Return 0Text Universe * use 1 for errors above version 4.3.9 of php

}

Return $this- > queryResult

}

/ * *

* data acquisition

* * /

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 was 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)

}

/ * *

* get the number of rows affected

* get the number of rows operated

* * /

Function num_rows ($result= "") {

If ($result! = "") {

$this- > queryResult = $result

$row = mssql_num_rows ($this- > queryResult)

Return $row

}

}

/ * *

* obtain query results-multiple

* * /

Function result_ar ($str='') {

If (empty ($str)) {

Return 0

}

$back = array ()

$this- > queryResult = $this- > query ($str)

While ($row = $this- > fetch_array ($this- > queryResult)) {

$back [] = $row

}

Return $back

}

/ * *

* Database information paging

* $Result database operation

* str = = sql statement

* page = which page

* showNum = = display a few pages

* /

Function page ($Str,$Page=0,$ShowNum=5) {

$back = array (); / / return data

$maxNum = 0

If ($Str = "") {

$this- > ER = "No data"

Return 0

}

$this- > queryResult = $this- > query ($Str)

If ($this- > queryResult) {

If ($Page== "") {

$nopa=0

} else {

$nopa = ($Page-1) * $ShowNum

If ($nopanum_rows ($this- > queryResult)

$kumb0

$iTun0

$dd=$this- > fetch_array ($this- > queryResult)

While ($dd&&$nopaqueryResult,$nopa)

$row=$this- > fetch_array ($this- > queryResult)

$nopa++

$iTunes +

$back [] = $row

If ($nopa > = $maxNum) {

Break

}

}

}

$this- > pageNum = $maxNum

Return $back

}

/ * *

* html page number for paging

* /

Function page_html ($DataNum=0,$Page=1,$ShowNum=3,$web,$Post='') {

If ($DataNum = = 0) {

$back = "No data to query"

} else {

If ($ShowNum > last page "

}

$back = <

After reading the above, do you have any further understanding of how to connect MSSQL in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 247

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report