PHP uses ADODB

Source: Internet
Author: User

Some days ago, I have been searching for PHP on the Internet for how to connect to SQL server2005 for a long time. I haven't found it. I also asked Baidu for a long time, or
No one knows how to connect to SQL server2005. It's still easy to use. I want to share it with you today when I publish it online.
Don't try to contact me like me. Let's talk about how to connect with SQL server2005.

In fact, the connection to SQL server2005 is very simple, it is to use the PHP COM object. It is also introduced in the PHP manual.

$ Mydsn = "provider = sqlncli.1; persist Security info = true; Data Source = $ server; user id = $ dbname; Password = $ dbpass; initial catalog = $ Database ";
$ Conn = new COM ('ADODB. connection ');
$ Conn-> open ($ mydsn );

In this way, you can connect to SQL server2005. If you are a friend of ASP, you should be familiar with it.

First use: $ conn = new COM ('ADODB. connection'); To create a conn object, we can use the ASP method.
And then open a connection in the user's open method. Because SQL server2005 and SQL Server2000
The SQL Server 2000 driver is sqloledb, and SQL server2005 is not the same as sqlnci.1. If
If your SQL server2005 is locally connected to localhost, you don't have to worry about it. If not, check whether your SQL server2005 has enabled remote connection.
Because SQL Server 2005 does not allow remote connection by default. To enable this function, click Start> program> Micrsoft SQL Server 2005.
Configuration tool-> peripheral application and Manager-> service and connected peripheral application configurator-> remote service-> select local connection and remote connection-> select
Use TCP/IP and Named Pipes
Well, I am executing the SQL statement below. I can execute the SQL statement or ADODB. recordset. We will first execute
To execute,
Haha, it takes a long time for Jack to figure it out.

Assume that a table named user has been created.
$ Conn = new COM ('ADODB. connection') or die ('Connection error ');
$ Mydsn = "provider = sqlncli.1; persist Security info = true; Data Source = localhost; user id = dbname; Password = dbpass; initial catalog = Database ";
$ Conn-> open ($ mydsn) or die ('invalid connection statement ');
$ SQL = "select * from [user]";
$ Rs = $ Conn-> execute ($ SQL );
If ($ RS-> EOF ()){
Print ('no data ');

} Else {
While (! $ RS-> EOF ()){
Echo $ RS-> fields [0]-> value;
$ RS-> movenext ();

}

}
Does it look like ASP operations? Other methods of ASP operations can be used here?
Let's take a look at ADODB. recordset.

$ Conn = new COM ('ADODB. connection') or die ('Connection error ');
$ Mydsn = "provider = sqlncli.1; persist Security info = true; Data Source = localhost; user id = dbname; Password = dbpass; initial catalog = Database ";
$ Conn-> open ($ mydsn) or die ('invalid connection statement ');
$ Rs = new COM ('ADODB. recordset ');
$ SQL = "select * from [user]";
$ RS-> open ($ SQL, $ Conn, 1, 3 );
If ($ RS-> EOF ()){
Print ('no data ');

} Else {
While (! $ RS-> EOF ()){
Echo $ RS-> fields (0)-> value;
$ RS-> movenext ();

}

}

Haha, it's just a little different. By the way, we can get the value of a field in ASP, for example, RS ("username"). It can also be obtained in PHP.
For example, $ Rs ['username'] can also get a value, but pay attention to a problem. If one of the values is null, PHP will report this
Sample error:Catchable fatal error: Object of class variant cocould not be converted to string inE:/web/wwwroot/teaweb/teahouses/include/index. Class. php. CSOn Line100
If you want to use is_null or empty to judge whether it is not possible, but it still reports errors, how can this problem be solved?
It's better to be flustered. If the field name to be obtained is username, check whether it is null.

If ($ RS-> fields ('username')-> value = NULL ){
Echo 'null ';
} Else {
Echo 'not null ';
}

This effectively determines whether it is null.

 

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.