Smarty Example Teaching examples (III., using ADODB connection database)
The first two months because of the reasons for the work has been very busy, so did not complete this tutorial in time, just today in Saturday do not have to work overtime, draw an empty finish it! At the beginning of a new tutorial, I
First of all I wrote the tutorial in the wrong place to change over, here to thank the Nesta2001zhang brothers, he found the article in some of the mistakes, or really someone else
Scold "fraught" (is really ashamed to say, my first draft after the release of the discovery in a lot of problems, and then some time after the change of the document also appeared in the wrong, 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 read:
=========================================================
while ($db->next_record () && $i > 0)
{
$array [] = Array ("NewsID" => $db->f ("Inewsid"),
"Newstitle" => csubstr ($db->f ("Vcnewstitle"), 0, 20));
$i--;
}
=========================================================
Why do you do it this way? Because the second method is clearer, the effect of the first approach is no different from the second, and I've debugged all the programs,
There is no problem.
Well, let's talk about ADODB today. Speaking of ADODB, may have done ASP all know the Windows platform ADO components, but our ADODB here is not the Microsoft database
As a component, but written by the PHP language of a set of database Operations class library, let us first look at what it has the advantages of the bottom.
1. Database execution code written in a standard SQL statement does not change the source program when performing a database migration, which means it can support multiple databases, including access.
2. Provides syntax functions similar to Microsoft ADODB. This is a great boon for people who switch from ASP to PHP, and many of its operations are similar to the ADODB in Windows.
3. You can generate a two-dimensional array of smarty loops, which simplifies smarty development. This is what I'm going to show you.
4. Support the database Cache query, the most likely to improve the speed of querying the database.
5. Other practical functions.
Although the advantages are many, but because the class library is very large, light its main executive class on the 107K, so if you consider the implementation of the efficiency of the need to seriously think about it. But honestly, it's
The function is still very powerful, there are a lot of very useful functions, use it's these functions, can be very convenient to achieve the function we want. So for those bosses who don't have special requirements,
Use it for protection
First, how to get ADODB? What is its operating environment?
From the http://sourceforge.net/project/show ... 簆 hp4.0.5 above.
Second, how to install ADODB?
Decompression download back to the compressed file, note: Everyone downloaded back to the format of ADODB.tar.gz, which is the compressed Linux format, in Windows you can use WinRAR to its
Row decompression, after the decompression is completed, copy the directory to the ADODB directory of the specified directory, as I copied it into the/comm/adodb/in the example.
Third, how to invoke ADODB?
Use Include_once ("./comm/adodb/adodb.inc.php"); is that a good line? Contains the ADODB Master file.
Iv. How to use ADODB?
1. To 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 build a new connection much like the standard connection in PHP:
$conn = new Adonewconnection ($dbDriver);
$conn->connect ($host, $user, $passwd, $db);
Easy, huh? If you have used a DB class in Phplib, you should be familiar with it.
The second way: the use of DSN, this is the database of the connection statement to write a statement to initialize, DSN is written as: $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, username: oracleuser, password is ORACLEPASSWD, database server is localhost, database is oradb DSN write this:
$DSN = "ORACLE://ORACLEUSER:ORACLEPASSWD@LOCALHOST/ORADB";
$conn = new Adonewconnection ($DSN);
This approach may be more interesting to programmers who switch from ASP.
Both of these methods can be used, it depends on the personal habits to choose.
2. Related concepts:
Using ADODB there are two basic classes, one is the ADOConnection class, the other is the Adorecordset class, and people who use ASP see these two classes to understand what it means.
ADOConnection refers to the database connection class, and Adorecordset refers to the ADOConnection execution query returns the DataSet class, the relevant information can be queried ADODB
The manual for the class.
3. Basic functions:
Related methods for the ADOConnection class are:
1.Connect: Database connection method, which we have described above. For MySQL and Pconnect, as in the PHP language
2.Execute ($sql): Execute query statement result returns a Adorecordset class.
3.GetOne ($sql): Returns the first field in the first row
4.GetAll ($sql): Returns all data. This function is of great use, remember what I did when I wrote about the news list in my previous tutorial, I want to show you what I need to see on the page
A two-dimensional array of news listings? 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 the list of news cases that will be displayed.
$array [0] = Array ("NewsID" =>1, "Newstitle" => "the first article of the press here");
$array [1] = Array ("NewsID" =>2, "Newstitle" => "the second article of 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 if we see it? The two-dimensional data I'm going to create manually here is directly using GetAll.!!! This is also for
What would anyone say Adodb+smarty is one of the reasons for the invincible combination ...
4.SelectLimit ($sql, $numrows =-1, $offset =-1, $inputarrr =false): Returns a DataSet, it is not difficult to see from the statement that it is a limited query language
Sentence, and the limit in the MySQL statement has the same effect, to a simple example:
$rs = $conn->selectlimit ("Select Inewsid, Vcnewstitle from Tb_news_ch", 5, 1);
Do you understand me? The $RS holds 5 records in the database starting with the first record. We know that the Oracle database does not support the use of limit in SQL statements, but if we make
With ADODB words, then this problem is easy to solve a lot of!
5.Close (): Close the database, although the end of the page, PHP will automatically shut down, but for the integrity of the program we still want to end the page in the closing of the database.
As for 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 included. This record determines the total number of records in the dataset.
3. GetMenu ($name, [$default _str= '], [$blank 1stitem=true], [$multiple _select=false], [$size =0], [$moreAttr = ']] very good one
function that you can use to return a name= $name drop-down menu (or multiple marquee)!!! Of course, it's an HTML string, which is an exciting good thing, $name refers to the
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 multiple-selection box, and we get this
String, you can use $smarty-> ("Templatevar", "Getmenustr") to enter a drop-down list (or multiple first 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 suit in Ms ADODB.
5. MoveFirst (), MoveLast (), Move ($to): The same, look at the function name everyone can know what it means.
6. Fetchrow (): Return one line to see the code:
=========================================================
$rs = & $conn->exceute ($sql);
if ($RS)
{
while ($row = $rs->fetchrow ())
{
$array [] = Array ("NewsID" => $row ["Inewsid"],
"Newstitle" => csubstr ($row ["Vcnewstitle"]), 0, 20);
}
}
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.