Php connection to mssql (com & amp; pdo)

Source: Internet
Author: User
Tags mssql server
To use COM1, first of all, preparations: Access MSSQLSERVER2000 through PHP in Windows, there are two ways, (1) using COM (2) one is to use the MSSQL _ series functions in php. ini settings: (1) to allow DCOM, you need to set php. com. remove the semicolon before allow_dcomTRUE. (2) Use

Use COM 1. First of all, preparations: access mssql server 2000 through PHP in Windows, there are two ways, (1) using COM (2) one is to use the MSSQL _ series functions in php. ini settings: (1) Allow DCOM, you need to set php. com. remove the Semicolon ";" before allow_dcom = TRUE. (2) Use

Use COM
1. preparations:
You can access mssql server 2000 through PHP in Windows in either of the following ways,
(1) Use COM
(2) MSSQL _ series Functions
To use these two methods, you need to set them in php. ini:
(1) To allow DCOM, remove the Semicolon ";" before com. allow_dcom = TRUE in php. ini.
(2) to use the MSSQL extension, remove the Semicolon ";" in php. ini before extension = php_mssql.dll.
(3) confirm that extension_dir is the correct path. Take the local machine as an example: extension_dir = "c:/AppServ/php5/ext ".
(4) If the machine still reports an error, c:/AppServ/php5/ext/php_mssql.dll cannot be found, but this file exists clearly.
Solution: Copy php_mssql.dll and ntwdblib. dll to the system directory/system32 and restart the test ..
(Note: The above two dll files are not in the same directory. My files are c:/AppServ/php5/ext/php_mssql.dll; c:/AppServ/php5/Ntwdblib. dll)
In addition, remember to restart the server after setting it.
My PHP environment was built with AppServ. After I did this, it was normal to access MSSQL SERVER. If you still have problems, please refer to the PHP manual php. ini settings.
2. A simple method to access mssql server using COM
$ Conn = new COM ("ADODB. Connection") or die ("Cannot start ADO ");
// Declare a connection object first
$ Connstr = "Provider = SQLOLEDB;
Persist Security Info = False;
User ID = youruid;
Password = yourpwd;
Initial Catalog = yourdatabase;
Data Source = 127.0.0.1 ";
// Set the connection string. (It is strange that an error is always reported when the Data Soure is localhost or the server name cannot be used, but the error is (local) or 127.0.0.1. Which of the following experts can help me explain the problem)
$ Conn-> Open ($ connstr); // create a database connection
$ Sqlstr = "select * from test"; // you can specify a query string.
$ Rs = $ conn-> Execute ($ sqlstr); // query the result.
Or
// $ Rs = new com ("adodb. recordset"); // declare a DataSet object
// $ Rs-> open ($ sqlstr, $ conn); //, 3, 3); // obtain the rcordset content of the dataset.

$ Num_cols = $ rs-> Fields-> Count (); // obtain the number of dataset Columns
While (! $ Rs-> EOF) // output result
{
Echo $ rs-> Fields ['name']-> Value ."
"; // The 'name' field name must be specified
$ Rs-> MoveNext ();
Or
// For ($ I = 0; $ I <$ num_columns; $ I ++)
//{
// Echo $ response [$ I]-> value. "/t ";
//}
// Echo "/n ";
// $ Rs-> MoveNext ();
}
$ Rs-> Close ();
$ Conn-> Close ();
$ Rs = null;
$ Conn = null;
3. Use the MSSQL _ function to access mssql server.
$ Hostname = "yourhoustname ";
$ Username = "yourusername ";
$ Password = "yourpassword ";
$ Conn = mssql_connect ($ hostname, $ username, $ password) or die ("database failed to respond .");
// Establish a connection
$ DbName = "yourdatabase ";
$ Ret = mssql_select_db ($ dbName); // select a database
$ Sqlstr = "select * from test"; // you can specify a query string.
$ Result = mssql_query ($ query); // execute the query to obtain the result.

While ($ line = mssql_fetch_row ($ result) // output the result
{
Echo "$ line [0], $ line [1]";
// Mssql_fetch_row returns an enumerated (enumerative) array each time until no data exists and false is returned
}
Or
// While ($ row = mssql_fetch_array ($ result ))
//{
// Echo $ row ["name"];
Or
// Echo $ row [0], $ row [1];
// Mssql_fetch_array returns a numeric index array or a related array each time until no data exists and false is returned.
//}

Question:
This is a summary of accessing mssql server through PHP. I hope it will be helpful to you. The first writing is a beginner. Please point out and make progress together.
Help:
1. When I set the connection string, why does Data Soure always report an error when it is localhost or the server name is not working? (local) or 127.0.0.1.
2. How can I escape "LIKE '%-'" (a percent sign and a short horizontal line in single quotes) when setting a query string?


Use pdo
1. preparations:

(1) To use the PDO extension, remove the Semicolon ";" in php. ini before extension = php_pdo.dll.
(2) to access MSSQL, you need to remove ";" before "extension = php_pdo_mssql.dll" in php. ini (if not, add it ).
Supplement: If you need to access MySQL, you need to remove ";" in php. ini before extension = php_pdo_mssql.dll.
Remove the ";" before the corresponding extension for other databases ";".
(3) confirm that extension_dir is the correct path. Take the local machine as an example: extension_dir = "c:/AppServ/php5/ext ".
(4) If the machine still reports an error, c:/AppServ/php5/ext/php_mssql.dll cannot be found, but this file exists clearly.
Solution: Copy php_mssql.dll and ntwdblib. dll to the system directory/system32 and restart the test ..
(Note: The above two dll files are not in the same directory. My files are c:/AppServ/php5/ext/php_mssql.dll; c:/AppServ/php5/ntwdblib. dll)
In addition, remember to restart the server after setting it.

2. Use PDO to access MSSQL SERVER
$ Username = yourusername;
$ Pwd = yourpassword;
Try
{
//?? Host = 127.0.0.1 available; (local) unavailable; localhost unavailable; MAXY unavailable
$ Dbh = new PDO ('mssql: host = 127.0.0.1; dbname = yourdatabase', $ username, $ pwd );
// Declare a PDO object and specify its connection string
$ Sqlstri = "select * from UpLow ";
// Specify the query string
Foreach ($ dbh-> query ($ sqlstri) as $ row)
// Execute the query to obtain the result and output it
{
Print_r ($ row );
Or
// Echo $ row ['name'];
// Specify the Field Output
}
}
Catch (PDOException $ e)
{
Print "Error !; ". $ E-> getMessage ()."
";
}

Http://blog.csdn.net/fkedwgwy/archive/2008/12/09/3484757.aspx

Related Article

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.