php5.6 use ODBC to link MSSQL (including each version) example

Source: Internet
Author: User
Tags dsn odbc mssql

php5.3 cannot use PHP's own MSSQL functions to link MSSQL later.

You need to install Microsoft's own php_sqlsrv, the link failed after installation.

First, configure the odbc+ system DSN data source

Then locate the ODBC Data Source Administrator (start → control Panel → administrative tools → data source (ODBC)), go to System DSN, and click Add

Select SQL Server--"Finish"

Set the data source name "test" (this is the DSN name you use when you connect using the PHP ODBC function), click the Server Drop-down box to select the servers (I was manually filled in localhost)

Select authentication Method-"Next"

Check "Change the default data as" → Click the dropdown box, select the database that has been built-"next" steps to keep the default settings.

Click "Test Data Source" If "Test success" is returned. It's OK.


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.


$con = Odbc_connect (' ODBC name ', ' username ', ' password ');

2, Connection mssql2000

$con = Mssql_connect (' Database address ', ' username ', ' password ');

3, Connection mssql2008


$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

$query = "SELECT Top 12 * Database name ORDER BY id DESC";

Third, create the query and take out the data

1. ODBC

$result = Odbc_do ($con, $query);
while (Odbc_fetch_row ($result))
{
$ Variable name = Odbc_result ($result, field name);
}

2, Connection mssql2000


$result = Mssql_query ($con, $query);
while ($row =mssql_fetch_array ($result))
{
$ Variable name = $row [Field name];
}

3, Connection mssql2008


$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.
Second, the test page

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>access DB test</title>
<body>
&LT;H1 align= "center" >connect DB test<?php
//
Database operation functions
//
function DeleteAll ($conn, $table)
{
$sql = ' DELETE from '. $table. ' WHERE 1=1 ';
Odbc_exec ($conn, $sql);
}
function ShowAll ($conn, $table)
{
$sql = ' SELECT ID, BID, bname from '. $table;
$rs = Odbc_do ($conn, $sql);
print ' <table border= ' 1 ' > ';
print ' <tr><td> '. Odbc_field_name ($rs, 1). ' </td><td> '. Odbc_field_name ($rs, 2). ' </td><td> '. Odbc_field_name ($rs, 3). ' </td></tr> ';
while (Odbc_fetch_row ($RS))
{
print ' <tr> ';
print ' <td> '. Odbc_result ($rs, ' id '). ' </td> ';
print ' <td> '. Odbc_result ($rs, ' BID '). ' </td> ';
print ' <td> '. Odbc_result ($rs, ' bname '). ' </td> ';
print ' </tr> ';
}
print ' </table> ';
}
function Inserttestdata ($conn)
{
static $i = 0;
for ($j = 0; $j < $j + +)
{
$INSERTSQL = ' INSERT into book (BID, bname) VALUES (\ ' ISBN 7-5325-2480-', $i. ', ' ' test '. $i + +. ') \')';
Odbc_exec ($conn, $INSERTSQL);
}
}
function CreateTable ($conn)
{
$tableRs = Odbc_tables ($conn, NULL, NULL, ' book ', ' TABLE ');
if (Odbc_fetch_row ($tableRs))
{
Return ' table already exists. '
}
$CREATESQL =
(' CREATE TABLE book '.
'('.
' ID INT IDENTITY (1, 1) PRIMARY KEY, '.
' BID VARCHAR not NULL, '.
' Bname NVARCHAR not NULL, '.
')'
);
Odbc_do ($conn, $CREATESQL);
Return ' table created! ';
}
//
Test process
//
print ' <pre>connecting db....</pre> ';
$conn = Odbc_connect ("Test", "sa", "password", Sql_cur_use_odbc) or Die (' fail to connect DB ');
/**
* Build the table and display the results
*/
Print CreateTable ($conn). ' <br> ';
Print "Table is:<br>";
$tableRs = Odbc_tables ($conn, NULL, NULL, ' book ', ' TABLE ');
while (Odbc_fetch_row ($tableRs))
{
Print Odbc_result ($tableRs, ' table_name '). ' <br> ';
}
/**
* Insert test data
*/
Inserttestdata ($conn);
print ' ShowAll ($conn, ' book ');
/**
* Delete Data
*/
DeleteAll ($conn, ' book ');
print ' Delete all data: <br> ';
ShowAll ($conn, ' book ');
/**
* Close Data connection
*/
Odbc_close ($conn);
?>
</body>

Close connection

There is no difference between the Odbc_close () and the Mssql_close () and sqlsrv_close ();

Well, the above command is that we have to organize for you oh, I hope the article can help you.

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.