Home > Backend Development > PHP Tutorial > ADODB combined with SMARTY ~ super powerful_PHP tutorial

ADODB combined with SMARTY ~ super powerful_PHP tutorial

WBOY
Release: 2016-07-21 15:59:48
Original
940 people have browsed it

Smarty practical teaching examples (3. Connecting to the database using ADODB)

I have been very busy in the past two months due to work reasons, so I did not complete this tutorial in time. It just so happened that I didn’t have to work overtime today on Saturday, so I took the time to complete it. It! When starting a new tutorial, I

first corrected some errors in the tutorial I wrote before. I would like to thank brother nesta2001zhang for finding out the article. There are some mistakes in it, otherwise others

would really scold me for "misleading people" (I'm really ashamed to say that after my first draft was released, I found a lot of problems, and later revised it some time) There are also errors in the files, which should not be done

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--;
}
==== ================================================== ===
should be changed to:
======================================== ====================
while($db->next_record() && $i > 0)
{
$array[ ] = array("NewsID" => $db->f("iNewsID"),
"NewsTitle" => csubstr($db->f("vcNewsTitle"), 0, 20)) ;

$i--;
}
================================ ==========================
Why change it like this? Because the second method is clearer, in fact the first method The effect is no different from the second method, and I have debugged those programs before.

There are no problems.
Okay, let’s talk about ADODB today. When it comes to ADODB, those who have done ASP may know the ADO component of the WINDOWS platform, but the ADODB we have here is not the database operation component of Microsoft, but a set of database operation class libraries written in the PHP language. Let us first take a look at what advantages it has.
1. The database execution code written in standard SQL statements does not need to change the source program when transplanting the database, which means that it can support a variety of databases. Including ACCESS.
2. Provides syntax functions similar to Microsoft ADODB. This is a great boon for people who switch from ASP to PHP. Many of its operations are similar to ADODB in WINDOWS.
3. Yes Generating the two-dimensional array required for Smarty loops will simplify smarty development. I will demonstrate this to you later.
4. Support cached queries of the database to maximize the speed of querying the database.
5. Other practical functions.
Although it has many advantages, because this class library is very large, its main execution class alone is 107K, so if you consider execution efficiency, you must think about it seriously. But to be honest, its

functions are still very powerful and have many very practical functions. Using these functions, we can easily achieve the functions we want. So there are no special requirements for those bosses Don’t use it if you don’t



1. How to get ADODB? What is its operating environment?
From http://sourceforge.net/project/show... hp4.0.5 or above.
2. How to install ADODB?
Decompress the downloaded compressed file. Note: The format you downloaded is ADODB.tar.gz, which is the compression format of Linux. Under Windows, you can use winrar to compress it.

Execute the decompression. After decompression is completed, copy the directory to the adodb directory of the specified directory. Like I copied it to /comm/adodb/ in the example.
3. How to call ADODB?
Use include_once ("./comm/adodb/adodb.inc.php"); This line goes without saying, right? Contains the main file for ADODB.
4. How to use ADODB?
1. Initialization:
ADODB uses the statement $conn = ADONewConnection(); to initialize. There are two ways to initialize ADODB:
The first way The method is: traditional method. I'll call it that for now. The method it uses to establish a new connection is very similar to the standard connection method in PHP:
$conn = new ADONewConnection($dbDriver);
$conn->Connect($host, $user, $passwd, $db);
Simple, right? If you have used the db class in phplib, you should be familiar with it.

The second method: use the dsn method, which writes the database connection statement into one statement for initialization. The way to write dsn is as follows: $dsn =

"DBType://User :Passwd@Host/DBName"; where DBType represents the database type, User represents the user name, Passwd is the password, Host is the server name, DBName is the database name

, like this I use the oracle database, user name: oracleUser , the password is oraclePasswd, the database server is localhost, and the dsn of the database is oradb is written like this:
$dsn = "oracle://oracleUser:OraclePasswd@localhost/oradb";
$conn = new ADONewConnection($dsn) ;
This method may be more interesting to programmers who have switched from ASP.

Both methods can be used, it depends on personal habits.

2. Related concepts:
There are two basic classes for using ADODB, one is ADOConnection class, the other is the ADORecordSet class. People who have used ASP will understand its meaning when they see these two classes.

ADOConnection refers to the database connection class, and ADORecordSet refers to the query statement executed by ADOConnection. For the returned data set class, you can check the ADODB

class manual for related information.

3. Basic functions:

The related methods of the ADOConnection class are:
1.Connect: Database connection method, which we introduced above. For mysql, there is also PConnect, which is the same as the usage in PHP language
2.Execute($sql): Execute the query statement and return an ADORecordSet class.
3.GetOne($sql): Returns the first field of the first row
4.GetAll($sql): Returns all data. This function is very useful. Do you remember that when I wrote about the input of the news list in the previous tutorial, I had to make the

news list that needs to be displayed on the page into a two-dimensional array? This is the sentence:
============================================ ===========================================
while($db ->next_record())
{
$array[] = array("NewsID" => $db->f("iNewsID"),
"NewsTitle" => csubstr( $db->f("vcNewsTitle"), 0, 20));
}
========================== ================================================== ==========
What does this line mean? It is to generate the news example table to be displayed
$array[0] = array("NewsID"=>1, "NewsTitle"=>"The first news item here");
$array[1 ] = array("NewsID"=>2, "NewsTitle"=>"The second article of news here");
...
This form, but if we don’t need to control the title , we are lucky 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 here should have been initialized. I wonder if you understand it? It turns out that the two-dimensional data I want to create manually can just use GetAll here! ! ! This is also one of the reasons why some people say that ADODB+Smarty is an invincible combination...
4.SelectLimit($sql, $numrows=-1, $offset=-1, $ inputarrr=false): Returns a data set. It is not difficult to see from the statement that it is a limited query

sentence, which has the same effect as the limit in the mysql statement. Here is a simple example:
$rs = $conn->SelectLimit("select iNewsID, vcNewsTitle from tb_news_CH", 5, 1);
Do you understand? Saved in $rs are the 5 records starting from the first record in the database. We know that the Oracle database does not support the use of limit in SQL statements, but if we use ADODB, then this problem will be much easier to solve!
5.Close(): Close the database. Although PHP will automatically close when the page ends, for the sake of the integrity of the program, you still have to close the database at the end of the page.

Regarding the results returned by ADORecordSet.ADORecordSet for $conn->Execute($sql), its basic functions are as follows:
1. Fields($colname): Returns the value of the field.
2. RecordCount(): The number of records contained. This record determines the total number of records in the data set.
3. GetMenu($name, [$default_str=''], [$blank1stItem=true], [$multiple_select= false], [$size=0], [$moreAttr='']) is a very good

function, which can return a drop-down menu (or multi-select box) with name=$name! !! Of course, it is an HTML string, which is an exciting thing. $name refers to the name attribute of

option, $default_str is the default selected string, and $blank1stItem points out Whether the first item is empty, $multiple_select indicates whether it is a multi-select box, and after we get this

string, we can use $smarty->("TemplateVar", "GetMenuStr") to add the value to the template Enter a drop-down list (or multiple boxes) at "TemplateVar"
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? It’s very similar to the one in MS ADODB!
5. MoveFirst( ), MoveLast(), Move($to): Same, you can know what it means by looking at the function name.
6. FetchRow(): Returns a row, look at the code:
==== ================================================== ===
$rs = &$conn->Exceute($sql);
if($rs)
{
while($row = $rs->FetchRow())
{
$array[] = array("NewsID" => $row["iNewsID"],
"NewsTitle" => csubstr($row["vcNewsTitle"]), 0, 20);
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317251.htmlTechArticleSmarty Example Teaching Example (3. Using ADODB to connect to the database) In the first two months, I have been busy due to work reasons. I'm busy, so I didn't finish this tutorial in time. It just so happens that I don't have to work overtime today on Saturday...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template