PHP connect MSSQL database beginner PHP notes

  • 2020-03-31 20:24:49
  • OfStack

 
<?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 PHP and MySQL link is implemented, PHP and MSSQL link is actually very simple;
Support MSSQL local links and remote links, local links for example:
Installed on the machine MS SQLServer 2005;
Configure the system before connection:
1. Check the file php5.2.5\ntwdblib. DLL below by default, can't connect 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 annotation for extension=php_mssql.dll.
B. Open php.in to change mssql.secure_connection = Off to on.
C, copy php_mssql.dll to the directory specified in extension_dir in php.in or under system32. Php_mssql.dll is available in the zip installation package for PHP.
You need to restart apache after the above steps are completed.
Note: in practice, it was found that if you manually install PHP under iis using a PHP zip file, you must restart the machine and not just iis.
3. The configuration used
A. Run SQL Server Configuration Manager: SQL Server Configuration Manager, and open the protocol
B. Allow named pipes and TCP/IP
C. Right-click on "TCP/IP" to open the Properties TAB "IP addresses"
D. Fill in 1433 at "TCP Dynamic Ports"
E. restart SQL Server


4. Connect MS SQL Server 2005 by:
The code is as follows:
 
<?php 
//Linked database
$conn=mssql_connect('localhost','sa','123456'); 
mssql_select_db('gu_dde',$conn); 
//The query statement
$Query="select * from dde_top"; 
$AdminResult=mssql_query($Query); 
//The output
$Num=mssql_num_rows($AdminResult); 
for($i=0;$i<$Num;$i++) 
{ 
$Row=mssql_fetch_array($AdminResult); 
echo($Row[1]); 
echo("<br/>"); 
} 
?> 

To see that these functions and MySQL functions are corresponding to the use of very convenient!

5. Frequently asked questions:
1 an error:
Fatal error: Call to undefined function mssql_connect()
Solution:
Use the MSSQL_ series of functions
To use both requires a php.ini setting:
(1) allow DCOM, need to put php.ini; Com.allow_dcom = semicolon before TRUE ";" Get rid of.
(2) use MSSQL extension, need php.ini; Extension =php_mssql.dll before the semicolon ";" Get rid of. (key)
(3) make sure that extension_dir is the correct path, with the native example: extension_dir = "c:\AppServ\php5\ext".
(4) if the machine still reports an error that it cannot find c: AppServ \php5\ext\php_mssql.dll but the file exists.
Solution: copy php_mssql.dll,ntwdblib.dll to system directory \system32 to restart the test.
(note: the above two DLL files are not in the same directory, mine is c:\AppServ\php5\ext \php_mssql.dll; C: \ AppServ \ php5 \ ntwdblib DLL)
In addition, remember to restart the server after setting up.
6. Other questions:
If PHP, apache, Sql, Server2000 were all on the same machine, access would be fine.
If the Sql Server2000 and PHP machines are separate, you need to make sure that the machine name of the machine on which the ping Sql server resides is passable. If not, modify the hosts file under \system32\drivers\etc of the machine on which PHP resides and add a line of machine name of the machine IP of the machine on which the Sql server resides.
If still unable to access, you need to confirm that the machine on which the PHP is located has mdac hidden. Why not simply install the sqlserver client.

Related articles: