First, remove the semicolon in the php.ini file, Extension=php_pdo_mssql.dll Extension=php_pdo_odbc.dll, and the corresponding way to connect MSSQL. Be aware that you want to restart the service for it to take effect.
First, establish the connection
1. ODBC
First, set up ODBC on the server where the PHP program resides. There are differences between 32-bit and 64-bit operating systems here. 32-bit from the Control Panel management tools in the data Source (ODBC) directly built on it, 64-bit to run C:\Windows\SysWOW64\odbcad32.exe
Set from here. Note: The above only is the database server for 32, the data source set the server to 32-bit and 64-bit situation. As long as two servers establish a consistent number of data source digits.
The following is the ODBC establish connection code.
Copy Code code as follows:
$con = Odbc_connect (' ODBC name ', ' username ', ' password ');
2, Connection mssql2000
Copy Code code as follows:
$con = Mssql_connect (' Database address ', ' username ', ' password ');
3, Connection mssql2008
Copy Code code as follows:
$connectionInfo = Array ("UID" => username, "PWD" => password, "DB" => "database name");
$con = sqlsrv_connect (database address, $connectionInfo);
Second, the input query code
This is the same, you can write directly, can also be verified from the MSSQL copied over. The simple point is to assign an SQL statement to a variable.
Similar to the following code
Copy Code code as follows:
$query = "SELECT Top 12 * Database name ORDER BY id DESC";
Third, create the query and take out the data
1. ODBC
Copy Code code as follows:
$result = Odbc_do ($con, $query);
while (Odbc_fetch_row ($result))
{
$ Variable name = Odbc_result ($result, field name);
}
2, Connection mssql2000
Copy Code code as follows:
$result = Mssql_query ($con, $query);
while ($row =mssql_fetch_array ($result))
{
$ Variable name = $row [Field name];
}
3, Connection mssql2008
Copy Code code as follows:
$result = sqlsrv_query ($con, $query);
while ($row = Sqlsrv_fetch_array ($result))
{
$ Variable name = $row [Field name];
}
The SQLSRV library is not included with the php5.3 and later versions. So I want to download it from Microsoft.
Four, close the connection
There is no difference between the Odbc_close () and the Mssql_close () and sqlsrv_close ();
Finally realize: The PHP connection MSSQL is less than the function that connects MSSQL, but also enough. Specific functions can refer to the official PHP manual