Use ADODB in combination with SMARTY ~ Super strong

Source: Internet
Author: User
Use ADODB in combination with SMARTY ~ Super strong Smarty instance teaching example (III. using ADODB to connect to the database)

I have been busy for work for the first two months, so I didn't finish this tutorial in time. I don't have to work overtime on Saturday. I just need to leave it empty! When I started a new tutorial

First, modify some of the errors in the previous tutorial I wrote. here, I would like to thank the nesta2001zhang brothers for finding some errors in the article. Otherwise, they will be replaced by others.

Scold "it's wrong (it's really embarrassing. after my first draft was published, I found a lot of problems. later, some times I resend the modified file and there were also errors. I really shouldn't

This ...)
In the previous tutorials:
========================================================== ========================
While ($ db-> next_record () & $ I> 0)
{
$ Array [] = array ("NewsID", csubstr ($ db-> f ("iNewsID"), 0, 20 ),
"NewsTitle", csubstr ($ db-> f ("vcNewsTitle"), 0, 20 ));

$ I --;
}
========================================================== ========================
It should be changed:
========================================================== ========================
While ($ db-> next_record () & $ I> 0)
{
$ Array [] = array ("NewsID" => $ db-> f ("iNewsID "),
"NewsTitle" => csubstr ($ db-> f ("vcNewsTitle"), 0, 20 ));

$ I --;
}
========================================================== ========================
Why? Because the second method is clearer and clearer, in fact, the effect of the first method is no different from that of the second method, and I have debugged these programs,

No problem.
Well, let's talk about ADODB today. when it comes to ADODB, ASP users may all know the ADO components on the WINDOWS platform, but the ADODB here is not the database operation of Microsoft.

As a component, but a set of database operation class libraries written by the php language. let's take a look at its advantages.
1. the database execution code written with standard SQL statements does not need to change the source program during Database transplantation. that is to say, it supports multiple databases, including ACCESS.
2. provide similar syntax functions to Microsoft ADODB. this is a good news for people switching from ASP to PHP. many of its operations are similar to ADODB in WINDOWS.
3. the two-dimensional array required by the Smarty loop can be generated, which simplifies the development of smarty. this will be demonstrated by me later.
4. supports database cache queries to increase the speed of database queries.
5. other practical functions.
Although there are many advantages, this class library is very huge, and its main execution class is only 107 K. Therefore, if you consider the execution efficiency, you should think about it carefully. but to be honest, its

The functions are still very powerful, and there are many very practical functions. using these functions can easily implement the functions we want. so when there are no special requirements for those bosses

Use it for defense

1. how to obtain ADODB? What is its runtime environment?
From http://sourceforge.net/project/show..w.hp4.0.5.
II. how to install ADODB?
Decompress the downloaded compressed file. note: the downloaded file format is adodb.tar.gz. this is a linux compression format. in windows, you can use winrar to import it.

Extract the directory and copy it to the adodb Directory of the specified directory. for example, I copied it to/comm/adodb/In the example.
3. how to call ADODB?
Use include_once ("./comm/adodb. inc. php"); isn't that necessary? The main file that contains ADODB.
4. how to use ADODB?
1. perform initialization:
ADODB uses a statement such as $ conn = ADONewConnection (); to initialize ADODB in two ways:
The first method is the traditional method. I will temporarily call it this name. The method used to create a new connection is similar to the standard connection method in php:
$ Conn = new ADONewConnection ($ dbDriver );
$ Conn-> Connect ($ host, $ user, $ passwd, $ db );
Simple? If you have used the db class in phplib, you should be familiar with it.

Method 2: Use the dsn method. in this way, the database connection statement is written as a statement for initialization. the dsn statement is written as: $ dsn =

"DBType: // User: Passwd @ Host/DBName"; DBType indicates the database type, User indicates the User name, Passwd indicates the password, Host indicates the server name, and DBName indicates the database name.

In this way, I use the oracle database, the username is oracleUser, the password is oraclePasswd, the database server is localhost, and the database is the dsn of oradb:
$ Dsn = "oracle: // oracleUser: OraclePasswd @ localhost/oradb ";
$ Conn = new ADONewConnection ($ dsn );
Programmers may be more interested in this method.

Both methods can be used, depending on your habits.

2. related concepts:
There are two basic classes for using ADODB. one is the ADOConnection class and the other is the ADORecordSet class. people who have used ASP will understand the meaning of these two classes,

ADOConnection refers to the database connection class, and ADORecordSet refers to the dataset class returned by the query statement executed by ADOConnection. you can query the relevant information of ADODB.

Class manual.

3. basic functions:

The methods related to the ADOConnection class are:
1. Connect: database connection method, which we have introduced above. For mysql, PConnect is also used in the same way as PHP.
2. Execute ($ SQL): an ADORecordSet class is returned when the query statement is executed.
3. GetOne ($ SQL): returns the first field of the first row.
4. GetAll ($ SQL): returns all data. This function is very useful. remember that when I write a news list input in a previous tutorial, I need to display it on the page.

Is the news list made into a two-dimensional array? The statement is as follows:
========================================================== ========================================================== =====
While ($ db-> next_record ())
{
$ Array [] = array ("NewsID" => $ db-> f ("iNewsID "),
"NewsTitle" => csubstr ($ db-> f ("vcNewsTitle"), 0, 20 ));
}
========================================================== ========================================================== =====
What does this line mean? Generate the news example table to be displayed.
$ Array [0] = array ("NewsID" => 1, "NewsTitle" => "The first article in the news ");
$ Array [1] = array ("NewsID" => 2, "NewsTitle" => "The second piece of news ");
...
This form, but if we do not need to control the title, we will be blessed in ADODB. we can write like this:
========================================================== ========================================================== =
$ StrQuery = "select iNews, vcNewsTitle from tb_news_ch ";
$ Array = & $ conn-> GetAll ($ strQuery); // pay attention to this statement.
$ Smarty-> assign ("News_CH", $ array );
Unset ($ array );
========================================================== ========================================================== =
Of course, $ conn should have been initialized. do you understand it? It turns out that the two-dimensional data that I want to manually create can be directly used GetAll here !!! This is also

Some people will say that ADODB + Smarty is one of the reasons for the invincible combination...
4. selectLimit ($ SQL, $ numrows =-1, $ offset =-1, $ inputarrr = false)

Statement, which has the same effect as the limit statement in mysql. here is a simple example:
$ Rs = $ conn-> SelectLimit ("select iNewsID, vcNewsTitle from tb_news_CH", 5, 1 );
Do you understand? $ Rs stores five records starting from the first record in the database. We know that the oracle database does not support limit in SQL statements, but if we

If ADODB is used, this problem will be easily solved!
5. Close (): Close the database. Although PHP will automatically Close at the end of the page, you must Close the database at the end of the page to complete the program.

The basic functions of ADORecordSet. ADORecordSet are as follows:
1. Fields ($ colname): return the value of the field.
2. RecordCount (): number of records included. this record determines the total number of records in the dataset.
3. getMenu ($ name, [$ default_str = ''], [$ blank1stItem = true], [$ multiple_select = false], [$ size = 0], [$ moreAttr = '']) very good one

Function. you can use this function to return a drop-down menu (or multiple selection boxes) with name = $ name )!!! Of course, it is an HTML string. this is an exciting good thing. $ name refers

The name attribute of option. $ default_str is the default selected string. $ blank1stItem indicates whether the first item is null, and $ multiple_select indicates whether multiple selection boxes exist.

After the string, you can use $ smarty-> ("TemplateVar", "GetMenuStr") to enter a drop-down list (or multi-first box) in "TemplateVar" of the template)
4. MoveNext (): Let's look at a piece of code:
========================================================== ========================
$ Rs = & $ conn-> Exceute ($ SQL );
If ($ rs)
{
While ($ rs-> EOF)
{
$ Array [] = array ("NewsID" => $ rs-> fields ["iNewsID"],
"NewsTitle" => csubstr ($ rs-> fields ["vcNewsTitle"]), 0, 20 );

$ Rs-> MoveNext ();
}
}
========================================================== ========================
Do you understand? Like the one in ms adodb!
5. MoveFirst (), MoveLast (), Move ($ to): Same. you can see what the function name means.
6. FetchRow (): return a row. check the code:
========================================================== ========================
$ Rs = & $ conn-> Exceute ($ SQL );
If ($ rs)
{
While ($ row = $ rs-> FetchRow ())
{
$ Array [] = array ("NewsID" => $ row ["iNewsID"],
"NewsTitle" => csubstr ($ row ["vcNewsTitle"]), 0, 20 );
}
}

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.