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

Php how to connect MSSQL, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

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

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: 260

*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