Shimokita glory days ADODB combined with Smarty use ~ Super strong

Source: Internet
Author: User
Tags php language
Smarty Example Teaching example (third, using ADODB connection database)
The first two months because of the cause of the work has been very busy, so did not complete the tutorial in time, just today Saturday without overtime, smoke a blank to complete it! At the beginning of the new tutorial, I
First of all, I wrote in the previous tutorial some of the wrong place to change, here to thank the Nesta2001zhang brothers, is he found some of the article errors, otherwise really be someone
Scold "fraught (say really ashamed, my first draft after the release found in a lot of problems, and then some time after the re-modified file unexpectedly also appeared errors, really should not
The ...)
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 did you change it? Because the second method is clearer, the first way is actually the same as the second method, and I've debugged those programs,
There is no problem.
Well, let's talk about ADODB today. When it comes to ADODB, it's possible that ASP knows the ADO components of the Windows platform, but our ADODB here is not the Microsoft database.
As a component, instead of a database Operation class library written in PHP, let's look at what the upside is.
1. Database execution code written in standard SQL statements does not have to change the source program when the database is migrated, which means it can support multiple databases, including access.
2. Provides syntax features similar to Microsoft ADODB. This is a great boon for people who switch from ASP to PHP, and many of the operations are similar to the ADODB in Windows.
3. You can generate a two-dimensional array that is required for the Smarty Loop, which simplifies smarty development. This is what I'm going to show you.
4. Support the database Cache query, the maximum possible increase the speed of querying the database.
5. Other useful functions.
Although there are many advantages, but because this kind of library is very large, light its main execution class is 107K, so if you consider the implementation of efficiency, you need to think carefully. But honestly, it's
function is still very powerful, there are a lot of very useful features, using its features, can be very convenient to achieve the functions we want. So for those bosses who don't have a special request, they don't
Use it for protection.
First, how to get ADODB? What is its operating environment?
From Http://sourceforge.net/project/show ... 簆 hp4.0.5 above.
Second, how to install ADODB?
Unzip the download back to the compressed file, note: Everyone download back the format for ADODB.tar.gz, this is the compression format of Linux, under Windows Everyone can use WinRAR to it into
Extract the directory into the ADODB directory of the specified directory, as I copied it to/comm/adodb/in the example.
Third, how to call ADODB?
Use Include_once ("./comm/adodb/adodb.inc.php"); that's not the line, is it? The primary file that contains the ADODB.
Iv. How to use ADODB?
1. Initialize:
ADODB uses $conn = Adonewconnection (), such statements are initialized, and there are two ways to initialize ADODB:
The first way is: the traditional way. I call it the name for the time being. It uses a way to create a new connection much like the standard connection in PHP:
$conn = new Adonewconnection ($dbDriver);
$conn->connect ($host, $user, $passwd, $db);
Simple, huh? If you have used the DB class in Phplib, you should be familiar with it.
The second way: the use of DSN mode, so that the database connection statement written as a statement to initialize, DSN has the following: $dsn =
"Dbtype://user:passwd@host/dbname"; Where DbType represents the database type, user represents the username, passwd is the password, host is the server name, dbname is the database name
, like this I use Oracle database, user name: Oracleuser, password is ORACLEPASSWD, database server is localhost, database for oradb DSN writes:
$DSN = "ORACLE://ORACLEUSER:ORACLEPASSWD@LOCALHOST/ORADB";
$conn = new Adonewconnection ($DSN);
Programmers who are likely to switch from ASP to this approach will be more interested.
Both of these methods can be used, depends on personal habits to choose.
2. Related concepts:
There are two basic classes using ADODB, one is the ADOConnection class, the other is the Adorecordset class, and the person who used the ASP sees that the two classes will understand its meaning,
ADOConnection refers to the class of database connection, and Adorecordset refers to the data set class returned by ADOConnection execution query statement, the relevant information can be queried ADODB
The Manual of the class.
3. Basic functions:
The relevant methods for the ADOConnection class are:
1.Connect: Database connection method, above we introduced. For MySQL and Pconnect, as in the PHP language
2.Execute ($sql): Executes a query statement result returns a Adorecordset class.
3.GetOne ($sql): Returns the first field of the first row
4.GetAll ($sql): Returns all data. This function is very useful, remember I wrote in the previous tutorial about the input of the news list will need to be displayed on the page
The news list is made into a two-dimensional array? This is the statement:
=====================================================================================
while ($db->next_record ())
{
$array [] = Array ("NewsID" = = $db->f ("Inewsid"),
"Newstitle" = Csubstr ($db->f ("Vcnewstitle"), 0, 20));
}
=====================================================================================
What does this line mean? is to generate the news sample table that will be displayed
$array [0] = Array ("NewsID" =>1, "newstitle" = "The first article of the news here");
$array [1] = Array ("NewsID" =>2, "newstitle" = "The second article of the news here");
...
Such a form, but if we do not need to control the title, in the ADODB we are blessed, we can write:
==================================================================================
$strQuery = "Select INews, Vcnewstitle from Tb_news_ch";
$array = & $conn->getall ($strQuery);//Note this statement
$smarty->assign ("News_ch", $array);
Unset ($array);
==================================================================================
Of course, the $conn here should be initialized, I do not know you see it? Originally I want to manually create two-dimensional data here directly using GetAll on the line!!! This is also for
What someone would say Adodb+smarty is one of the reasons for the 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 language
Sentence, which is similar to the limit in the MySQL statement, comes in a simple example:
$rs = $conn->selectlimit ("Select Inewsid, Vcnewstitle from Tb_news_ch", 5, 1);
Did you see that? The $rs saves 5 records in the database starting with the first record. We know that the use of limit in SQL statements is not supported in Oracle databases, but if we make
With ADODB words, that problem is easier to solve more!
5.Close (): Close the database, although PHP will automatically close at the end of the page, but in order to complete the program you still have to close the database at the end of the page.
As for the result of Adorecordset.adorecordset returning 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 for the dataset.
3. GetMenu ($name, [$default _str= "], [$blank 1stitem=true], [$multiple _select=false], [$size =0], [$moreAttr = ']) very good
function, which can be used to return a name= $name drop-down menu (or a multi-marquee)!!! Of course, it's an HTML string, which is an exciting good thing, $name refers to
Option's Name property, $default _str is the default selected string, $blank 1stItem indicates whether the first item is empty, $multiple _select indicates whether it is a multi box, and we get this
String, you can use $smarty-> ("Templatevar", "Getmenustr") to enter a drop-down list (or multiple boxes) at the "Templatevar" of the template.
4. MoveNext (): Take a look at the 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 like the one in Ms ADODB!
5. MoveFirst (), MoveLast (), Move ($to): Same, look at the function name everyone can know what it means.
6. Fetchrow (): Return a line, 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);
}
}

The above describes the Shimokita glory days ADODB combined with Smarty use ~ Super strong, including the Shimokita glory days content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.