Before class, the popularization of small Knowledge: MSSQL and SQL Server is the same software, the name is different, MSSQL is Microsoft SQL Server,mssql is shorthand, some people like to call SQL Server directly, I prefer this name, have charm 、、、
Recently there are users in the use of Phpwamp, ask me a question, is about how PHP connect MSSQL database.
Normally we build a website is usually a php+mysql database,
However, in the project, we sometimes have to use the Php+mssql database, what should be done?
The case of the PHP integration environment is my latest release PHPWAMP8.1.8.8, regardless of whether you use other integrated environment, or install yourself, the operation method is the same, but I still recommend that you use my phpwamp, I integrated the components are full version, perfect error-free saving effort, put on the U disk at any time use (Support custom PHP version, multi-version running simultaneously)
In fact, I had the intention of the MSSQL database is also green to the PHP integration environment, without the installation of direct use.
But MSSQL database facing enterprises is charged, so I do not dare to green, in case of being held up, I was confused forced.
You can install the database on your own first, follow my way to connect it, I will show the whole process in detail later.
First, demonstrate the common way to build a local test, and then demonstrate how the site is formally run on the server.
In fact, there is no difference, mainly to demonstrate the general usage and site management inside the usage, let you more skilled use of it.
First of all to show you the php5.3 connection MSSQL database of the specific approach (database is SQL Server 2008)
In Phpwamp, the point here is the default startup is php5.3, click Start (Other PHP version, please switch on your own)
After launching, click here to browse the website
At the bottom of this page, click on "Click here to view Phpinfo file" To view more detailed environmental content.
Came to the Phpinfo file page, see here the thread safety shows enabled,
Thread safety is threaded, and enabled is the meaning of boot, which means that it is currently thread-safe.
If the thread safety is not enabled, but disabled, then it is non-thread safe.
Look at the picture very clearly, is currently thread-safe, and is VC6
PHP5.3 more than the version, the connection MSSQL database is no longer the mssql.dll extension, instead of the sqlsrv.dll extension.
Because you want to select the corresponding driver, to determine whether it is non-thread-safe or thread-safe, Microsoft provides the relevant driver,
For PHP support MSSQL database. So I have so many versions of the driver, which one should I choose?
(PHP version support MSSQL database driver Please search this title in Baidu "PHP support MSSQL database driver, driver support all PHP version (most full)")
The PHP version we opened earlier is 5.3, then thread safe, and the Phpinfo page display is VC6
So the choice is Php_pdo_sqlsrv_53_ts_vc6.dll and Php_sqlsrv_53_ts_vc6.dll these two.
So many users say that the connection failed, is because you choose the driver version has a problem, to be the right seat.
Open the directory where the Phpwamp version file is located.
Go to the corresponding Ext directory and copy the Php_pdo_sqlsrv_53_ts_vc6.dll and Php_sqlsrv_53_ts_vc6.dll.
The two driver files are connected in the form of PDO, and the other is connected in a sqlsrv_connect way.
Copy it in as shown.
OK, then next, open the corresponding version of the PHP configuration file
In an open php.ini file
Extension=php_sqlsrv_53_ts_vc6.dll
Extension=php_pdo_sqlsrv_53_ts_vc6.dll
Add these two lines, and then search for "mssql.secure_connection" section,
Change "mssql.secure_connection = Off" to "mssql.secure_connection = on"
Once the modifications are complete, double-click here to restart the current Apache server.
After restarting, if you can find sqlsrv on the Phpinfo page, you have successfully configured it.
Next we will connect the database to see if it is normal!
Before testing the database, let's look at what the database port number is, right-click "New Query"
Enter EXEC Sys.sp_readerrorlog 0, 1, ' listening ' and then right click on Execute.
The discovery port number is the default of 1433, so we do not need to specify the port number when connecting to the database.
Because it is the default port, all we can write on the PHP page. Code casually dozen, prove that can connect on the line.
<?php try { $dbName = "Sqlsrv:server=127.0.0.1;database=lccee"; Here is the server IP address and database name, the port is not the default, remember to change $dbUser = "sa"; User name $dbPassword = "111111"; Login password $db = new PDO ($dbName, $dbUser, $dbPassword); if ($db) { echo "Congratulations! The database connection is successful!! <br/> "; } } catch (Exception $e) {echo "Database connection failed!! "; }? >
If it is not the default port, assuming 1688 port, then the above line should be changed.
$dbName = "Sqlsrv:server=127.0.0.1,1688;database=lccee";
Of course, even if the default port, you can also add the default number, is also true, as shown in the following code.
$dbName = "Sqlsrv:server=127.0.0.1,1433;database=lccee";
Easy error point: Here the ip+ port, not the colon! But a comma, if written "127.0.0.1:1433" is wrong!
OK, let's open this page to see the effect, run the code to indicate that the connection was successful.
Suppose I change the password of the database inside the connection, and then look at it, the prompt fails, such as.
We did not add two DLLs in the PHP configuration, respectively.
Php_pdo_sqlsrv_53_ts_vc6.dll and Php_sqlsrv_53_ts_vc6.dll
The two driver files are connected in the form of PDO, and the other is connected in a sqlsrv_connect way.
The connection we demonstrated above is PDO, and now we're going to connect it in a sqlsrv_connect way.
So the code can write, create a sqlsrv_connect.php file, and write the following code
<?php$servername = "NEPTUNE-PC"; Servername\instancename$connectioninfo = Array ("Database" = "Lccee", "UID" = "sa", "PWD" = "111111"); $conn = Sqlsrv_connect ($serverName, $connectionInfo); if ($conn) {echo "Connection succeeded <br/>";} else{echo "Connection failed <br/>"; }?>
Then test again to see that the connection was successful!
If I change the correct password 111111 to the wrong password 888888,
(Specific database password, see your own MSSQL database, my password is 111111)
Then the connection error is displayed, and when the correct password is changed to an error, the connection failure is displayed.
Let's change the code, read the contents of the database table, this is the database I created,
The database name is Lccee, and in this database there is a table called Phpwamp.
Then I randomly add a few lines of code to read the contents of the data table.
<?php$servername = "NEPTUNE-PC"; Servername\instancename$connectioninfo = Array ("Database" = "Lccee", "UID" = "sa", "PWD" = "111111"); $conn = Sqlsrv_connect ($serverName, $connectionInfo); if ($conn) {echo "Connection succeeded <br/>";} else{echo "Connection failed <br/>"; } $query = "select * from Phpwamp"; $result = sqlsrv_query ($conn, $query) and while ($row = Sqlsrv_fetch_array ($result)) {p Rint_r ($row); echo "<br>";}? >
Then save it, and then we'll take a look at it and read the contents of the data table after running.
Finally, let's demonstrate how sites created with site Administration on the server connect to the MSSQL database.
To begin our tutorial, the first step is to change the main interface to port 80, as shown in.
After modifying the port to switch to any PHP version under apache2.4, php5.5, php5.6, PHP7 can be.
Then open apache2.4 site management as shown in.
Establish a site, (Apache2.4 site management can establish countless sites, each site can specify a different version of PHP, you can also customize the PHP version, you can also run a number of different PHP versions at the same time, right-click Feature rich) below, I chose php5.4 this version, fill it out and click on the " Add Site "
Added site will appear on the left-hand site list, right-click site Select "Generate this station hosts content"
Then click the button here to run the site, as shown in the Red Arrows.
Right-click the site, select "Domain name browse this site", you can use the domain name to browse (on the server and then the domain name resolution on it)
If you do not understand how to parse, you can look at this article http://www.cnblogs.com/phpwamp/p/6282363.html
Click "Domain name browse this website", we can see this page, as shown in.
Click on the mouse to pull the scroll bar to see the more detailed phpinfo () information here.
You can see the Phpinfo file page, where the thread safety shows enabled,
Thread safety is thread-safe, and enabled is boot, then the description is currently threads safe.
The current PHP version is php5.4, so we should choose the two DLL files that are
Php_pdo_sqlsrv_54_ts.dll and Php_sqlsrv_54_nts.dll, such as
Copy the two DLLs into the corresponding PHP version of the Ext folder as before.
At this time we go back to the site management interface, right-click the site to open the corresponding php.ini with the file.
In an open php.ini file
Extension=php_sqlsrv_54_ts.dll
Extension=php_pdo_sqlsrv_54_ts.dll
Add the two lines above and search for the "mssql.secure_connection" section.
Change "mssql.secure_connection = Off" to "mssql.secure_connection = on"
After the modification, save, restart the Apache service again, create a sqlsrv_connect.php file, write the following code
<?php$servername = "NEPTUNE-PC"; Servername\instancename$connectioninfo = Array ("Database" = "Lccee", "UID" = "sa", "PWD" = "111111"); $conn = Sqlsrv_connect ($serverName, $connectionInfo); if ($conn) {echo "Connection succeeded <br/>";} else{echo "Connection failed <br/>"; } $query = "select * from Phpwamp"; $result = sqlsrv_query ($conn, $query) and while ($row = Sqlsrv_fetch_array ($result)) {p Rint_r ($row); echo "<br>";}? >
Then browsing the sqlsrv_connect.php file, you can read the information in the database, such as
Other PHP version is the same reason, leaf out can, if you have any questions to contact me.
If time permitting, I will also consider making a Linux under the Phpwamp, the requirements of users, Mac system is also in consideration, but the MAC system is not a special understanding of the need for a certain time research and research, the recent work is also very busy, a person time is limited, estimated progress will be very slow.
Later, I will be in the Phpwamp version of the configuration of the version of PHP and MSSQL connection, you will not have to configure the
PHP version support MSSQL database driver Please search this title in Baidu "PHP support MSSQL database driver, driver support all PHP version (most complete)"
PHP Connection MSSQL Database case, phpwamp multiple PHP versions to connect to the SQL Server database