Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PHP connects to MSSQL database case. Multiple PHP versions of PHPWAMP connect to SQL Server database.

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Popularization of pre-class knowledge: MSSQL and SQL Server are the same software, but they are called differently. The full name of MSSQL is Microsoft SQL Server,MSSQL, which is abbreviated, while some people like to call it SQL Server directly.

Recently, when using PHPWAMP, a user asked me a question about how PHP connects to MSSQL databases.

Usually we build a website, usually a PHP+Mysql database.

But in the project, we sometimes have to use the PHP+MSSQL database, so what should we do?

The PHP integrated environment used in this case is PHPWAMP8.1.8.8, and the latest version is PHPWAMP_IN2. Whether you use other integrated environment or install it yourself, the operation method is the same, but I still suggest you to use my PHPWAMP. All the components I integrate are complete, perfect, error-free, worry-saving and labor-saving, and can be used on USB disk at any time (support custom PHP version, multiple versions run at the same time)

In fact, I planned to green the entire MSSQL database into the PHP integrated environment earlier, so I don't need to install it and use it directly.

However, the MSSQL database is charged by enterprises, so I dare not make it green. In case of investigation, I will be confused.

You can install the database yourself and connect in my way. Later, I will demonstrate the whole process in detail.

First demonstrate the common construction methods of "local testing", and then demonstrate the formal construction of the website on the server.

In fact, there is no difference, mainly to demonstrate the regular usage and site management in the usage, so that you are more skilled in using it.

First, let's show you how php5.3 connects to the MSSQL database (the database is SQL Server 2008).

In PHPWAMP, click here to launch php5.3 by default, and click start (please switch between other PHP versions)

After launch, click here to browse the website

At the bottom of this page, click "Click here to view the phpinfo file" to see more detailed environment content.

Go to the phpinfo file page and see that the Thread Safety here shows enabled.

Thread Safety means thread safety, while enabled means startup, indicating that it is currently thread safe.

If Thread Safety does not show enabled, but disabled, then it is not thread safe.

If you look at the picture, it's obvious that it's thread-safe and VC6.

In versions above PHP5.3, connecting to MSSQL databases is no longer a mssql.dll extension, but is replaced by a sqlsrv.dll extension.

Because we have to choose the corresponding driver, Microsoft provides the relevant driver to determine whether it is non-thread-safe or thread-safe.

Used for PHP to support MSSQL databases. So I have so many versions of drivers below, which one should I choose?

(Lccee prompts you: these drivers are downloaded in the attachment to this article and include all versions of php.)

The PHP version we just opened is 5.3, and then it's thread-safe, and the phpinfo page shows VC6.

So you have to choose php_pdo_sqlsrv_53_ts_vc6.dll and php_sqlsrv_53_ts_vc6.dll.

So many users say that the connection failed because there is something wrong with the driver version you chose, so you have to sit in the right seat.

Open the directory where the PHPWAMP version file is located.

Go to the corresponding ext directory and copy php_pdo_sqlsrv_53_ts_vc6.dll and php_sqlsrv_53_ts_vc6.dll into it.

One of the two driver files is connected by pdo, and the other by sqlsrv_connect.

Copy it in as shown in the following figure.

OK, next, open the corresponding version of the PHP configuration file

In the open php.ini file

Extension=php_sqlsrv_53_ts_vc6.dll

Extension=php_pdo_sqlsrv_53_ts_vc6.dll

Add the above two lines, and then search for the paragraph "mssql.secure_connection"

Change "mssql.secure_connection = Off" to "mssql.secure_connection = On"

After the modification is completed, double-click here to restart the current apache server.

After restart, if sqlsrv can be found on the phpinfo page, it means that the configuration has been successfully completed, as shown in the figure.

Next, let's connect to the database to see if it is normal!

Before testing the database, let's take a look at the database port number and right-click "New query"

Type exec sys.sp_readerrorlog 0,1, 'listening' and then right-click execute.

It is found that the port number is 1433 by default, so we don't need to specify the port number when we connect to the database.

Because it is the default port, so we can write this on the PHP page. Just type the code casually and prove that it can be connected.

If it is not the default port, assuming port 1688, then the above line should be changed to.

$dbName = "sqlsrv:Server=127.0.0.1,1688;Database=lccee"

Of course, even if it is the default port, you can add the default port number, which is also true, as shown in the following code.

$dbName = "sqlsrv:Server=127.0.0.1,1433;Database=lccee"

Error prone: the ip+ port here is not a colon! It's a comma, and it would be wrong to write "127.0.0.1 purl 1433"!

OK, let's open this page to see the effect, run the code to indicate that the connection is successful, indicating that there is no problem.

Suppose I change the database password in the connection, and then look at it, and it will prompt me to fail, as shown in the following figure.

Didn't we add two DLL to the php configuration earlier, namely

Php_pdo_sqlsrv_53_ts_vc6.dll and php_sqlsrv_53_ts_vc6.dll

One of the two driver files is connected by pdo, and the other by sqlsrv_connect.

The connection method we demonstrated above is pdo, now let's connect in the way of sqlsrv_connect.

Then the code can be written like this, create a sqlsrv_connect.php file with the following code in it

Connect by sqlsrv_connect, if the port is not the default 1433

So here $serverName is written as $serverName = "127.0.0.1 1434"

Then test and see, the connection is also successful!

If I change the correct password 111111 to the wrong password 888888

(specific database password, look at your own MSSQL database, my password is 111111)

Then it shows that the connection is incorrect, and after changing the correct password to an error, it shows that the connection has failed.

Let's change the code again to read the contents of the database table. This is the database I created.

The database is called Lccee, and there is a table called phpwamp in this database.

Then I randomly add a few lines of code to read out the contents of the data table.

Then save it. At this time, let's take a look at it again. After running it, we will read out the contents of this data table.

Finally, let's demonstrate how a site created with site management on the server connects to the MSSQL database.

To start our tutorial, the first step is to change the main interface to port 80, as shown in the following figure.

After modifying the port, switch to any version of PHP under apache2.4, php5.5, php5.6, php7 can be.

Then open apache2.4 site Administration, as shown in the following figure.

Set up a site, as shown in the figure (Apache2.4 site management can build numerous sites, each site can specify a different PHP version, you can also customize the PHP version, and you can also run multiple different PHP versions at the same time, with rich right-click functions) as shown in the figure, I chose the php5.4 version. Click "add site" when you are finished.

After adding, the added site will appear in the list of sites on the left. Right-click the site and select "generate hosts content for this site".

Then click the button here to run the site, as shown in the red arrow below.

Right-click the site, select "Domain name Browse the website", and you can browse with the domain name (just do domain name resolution on the server)

If you don't know how to parse, you can take a look at my article: http://lccee.blog.51cto.com/10514884/1887604

After clicking "Domain name to browse the website", we can see this page, as shown in the following figure.

Pull the mouse down the scroll bar and click here to see more detailed phpinfo () information.

As you can see on the phpinfo file page, the Thread Safety here shows enabled

Thread Safety is thread-safe, while enabled is started, which means it is currently thread-safe.

The current version of PHP is php5.4, so the two DLL files we should choose are

Php_pdo_sqlsrv_54_ts.dll and php_sqlsrv_54_ts.dll, as shown below

Copy these two DLL into the ext folder of the corresponding PHP version as before.

At this time, we go back to the site management interface, right-click the site to open the corresponding php.ini configuration file.

In the open php.ini file

Extension=php_sqlsrv_54_ts.dll

Extension=php_pdo_sqlsrv_54_ts.dll

Add the above two lines and search for the paragraph "mssql.secure_connection".

Change "mssql.secure_connection = Off" to "mssql.secure_connection = On"

Save after modification, restart the Apache service again, and create a sqlsrv_connect.php file with the following code written in it

Then browse the sqlsrv_connect.php file and read out the information in the database, as shown in the following figure

The same is true for other PHP versions, just follow suit, and the relevant DLL can be downloaded in the attachment to this article.

The latest version of PHPWAMP integrated php5.2, php5.3, php5.4, php5.5, php5.6, and php7 all have MSSQL database extensions installed.

You can find sqlsrv on the phpinfo page, but you still can't connect to the database. You can download the ODBC driver and install it in the attachment.

In the future, if time permits, I will also consider making a PHPWAMP under Linux. At the request of netizens, the Mac system is also under consideration. However, I do not know much about the Mac system and need some time to study. Recently, I am also very busy with my work. I have limited time alone, and it is estimated that the progress will be very slow.

Later, I will configure the connection between each version of PHP and MSSQL in the PHPWAMP version, so you don't have to configure it yourself.

If you encounter any problems in the running environment, you can come to my blog to ask: http://lccee.blog.51cto.com/10514884/d-1

Final note: the DLL mentioned in the article is at the "attachment download" at the bottom of the article and contains all the drivers required for the PHP version.

Lccee teacher video tutorial: edu.51cto.com/lecturer/10514884.html

Other PHPWAMP application articles can be viewed here: https://blog.51cto.com/lccee

Welcome to follow my blog for the latest information, if you have any suggestions, you can trust me privately.

Attachment: http://down.51cto.com/data/2366396

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report