PHP connection MSSQL Database beginner PHP notes _php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
$serverSite = ".";
$db = "Phpdemo";
$name = "sa";
$pass = "sa";
$conn = @mssql_connect ($serverSite, $name, $pass) or Die ("Database connection Error!) ");
@mssql_select_db ("Phpdemo", $conn);
Echo ' This can be use! ';
$ok = @mssql_query ("INSERT into test (name) VALUES (' Ossem ')", $conn);
Echo ' This database is: '. $conn;
if ($ok)
{
echo "OK";
}else
{
echo "false";
}
?>

If you implement the PHP and MySQL links, PHP and MSSQL links in fact very simple;
Local links and remote links that support MSSQL, take local links for example:
MS SQL Server 2005 is installed on the machine;
Configure the system before connecting:
1. Check file Php5.2.5\ntwdblib.dll There is a default below, can not be connected to replace.
Download the correct version of Ntwdblib.dll (2000.80.194.0), Address: Http://webzila.com/dll/1/ntwdblib.zip
2. Configure PHP
A, open php.in to remove the extension=php_mssql.dll annotation symbol.
B, open php.in to change mssql.secure_connection = off to ON.
C, copy Php_mssql.dll to php.in extension_dir specified directory or system system32 directory. (Php_mssql.dll is available in PHP's compressed installation package).
You will need to restart Apache after completing these steps.
Note: In practice, if you manually install PHP to IIS by using PHP compressed files, you must restart the machine instead of just IIS.
3. Configure SQL Server
A. Run SQL Server Configuration Manager: SQL Server Configuration Manager, open protocol protocols
B. Allow Named pipes "Named Pipes" and "TCP/IP"
C. Right-click on "TCP/IP" to open the Property Properties tab "IPs addresses"
D. Fill in the TCP dynamic port "Ports" 1433
E. Restart SQL Server


4. Connect MS SQL Server 2005 using the following methods:
The code is as follows:
Copy Code code as follows:

<?php
Link Database
$conn =mssql_connect (' localhost ', ' sa ', ' 123456 ');
mssql_select_db (' Gu_dde ', $conn);
Query statement
$Query = "SELECT * from Dde_top";
$AdminResult =mssql_query ($Query);
Output results
$Num =mssql_num_rows ($AdminResult);
for ($i =0; $i < $Num; $i + +)
{
$Row =mssql_fetch_array ($AdminResult);
Echo ($Row [1]);
Echo ("<br/>");
}
?>

You can see these functions and MySQL functions are corresponding to the use is very convenient!

5.FAQ FAQ:
1 Error:
Fatal error:call to undefined function mssql_connect ()
Solve:
Using the Mssql_ series function
To use both of these needs to be set in php.ini:
(1) Allow DCOM, need to be php.ini in the com.allow_dcom=true before the semicolon ";" Remove.
(2) The use of MSSQL extension, need to php.ini in the Extension=php_mssql.dll before the semicolon ";" Remove. Key
(3) Confirm that the Extension_dir is the correct path and take this machine as an example: Extension_dir = "C:\AppServ\php5\ext".
(4) If the machine still complains that the C:\AppServ \php5\ext\php_mssql.dll is not found, but this file is clearly present.
Workaround: Php_mssql.dll,ntwdblib.dll Copy to the system directory \system32 restart the test.
(Note: The above two DLL files are not in the same directory, my C:\AppServ\php5\ext \php_mssql.dll;c:\appserv\php5\ntwdblib.dll)
In addition, remember to restart the server after setting.
6. Other issues:
If PHP Apache SQL Server2000 are all on the same machine, access is basically fine.
If the SQL Server2000 and PHP machine are separate, you need to confirm ping SQL Server machine name of the machine can pass, if not, modify the PHP machine \System32\Drivers\Etc under the Hosts file, add a line The machine name of the machine where SQL Server is located on the machine IP SQL Server.
If you still cannot access it, you need to verify that the machine on which PHP is located has Damin MDAC. or simply install the SQL Server client. OK.

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.