Your location:Tech News>News>Script>~ ADODB using super-strong binding SMARTY

Latest News

~ ADODB using super-strong binding SMARTY



  Smarty instance teaching example piece (Third, connect to the database using ADODB)

  The first two months have been very busy because of work, so there is no timely completion of this tutorial, not just today, Saturday overtime, pumping empty complete it! At the beginning of a new tutorial, I

  First the previous tutorial I wrote some modifications over the wrong place, here to thank nesta2001zhang brother, he is identified in the article some errors, or really by others

  Curse "fraught (really ashamed to say, I found that after the release of the first draft after a lot of problems, but then for some time to re-send the file after modification actually an error, really should not be


  On several tutorial:

  ================================================== =======

  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?The second method is more clear because some of the effect of the first way is actually performed with the second method makes no difference, and that several programs I have ever been debugging,

  There is no problem.

  Well, then we first is that ADODB today.Speaking ADODB, might have done ASP knows ADO components WINDOWS platform, but we ADODB here is not that Microsoft''s database operations

  As a component, but written by a set of database operations php language library, let us look at it they going, what kind of advantage.

  1. In a standard SQL database written statement to execute code without changing the source code during database migration, which means it can support a variety of databases, including ACCESS.

  2. Microsoft provides a similar function syntax ADODB.This is for the switch from ASP to PHP is a great boon for the people, many of its operations are similar to the WINDOWS ADODB.

  3. Smarty cycle can generate two-dimensional array of needs, this will simplify the development of smarty.This is so will I give you demo.

  4. Support database query cache, the maximum possible increase the speed of the database query.

  5. Other useful features.

  Although many advantages, but because this library is very large, light on its main execution class 107K, so if we consider the efficiency of the implementation, then you should seriously think about the.But honestly, it''s

  Function is very powerful, there are a lot of very useful features, use of these features it can be very easy to achieve our desired function.So for those bosses are no special requirements when we do not

  Anti-use with it

  First, how to get ADODB? What is its operating environment?

  From http: // / project / show.ENGLISH hp4.0.5 or more.

  Second, how to install ADODB?

  Extract the downloaded compressed file back, pay attention: you download format back to ADODB.tar.gz, which is linux compression format, you can use winrar in windows it into

  Line decompression, decompression after the completion of catalog copy to adodb directory specified directory, like in the example I will copy it to / comm / adodb / in.

  Third, how to call ADODB?

  Use include_once ( "https: // / article / comm / adodb / "); this line would not have said it?The master file contains ADODB.

  Fourth, how to use ADODB?


  ADODB using $ conn = ADONewConnection (); This initializes the statement of ADODB initialized in two ways:

  The first way is: the traditional way.I call it a temporary name.Establish a new connection much like the way it uses the standard php Connection:

  $ Conn = new ADONewConnection ($ dbDriver);

  $ Conn-> Connect ($ host, $ user, $ passwd, $ db);

  Simple, right?If you''ve used in phplib db class should be very familiar with it.

  The second method: dsn way, this statement is to connect a database written statement to initialize, dsn wording promising: $ dsn =

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

  Like this I use the oracle database, user name: oracleUser, password oraclePasswd, the database server is localhost, database oradb of dsn write:

  $ Dsn = "oracle: // oracleUser: OraclePasswd @ localhost / oradb";

  $ Conn = new ADONewConnection ($ dsn);

  This approach may be to switch from ASP programmers will be more interested in.

  Both approaches can be used, depending on personal habits to selected.

  2. Related concepts:

  Use ADODB There are two basic categories, one is ADOConnection class, and the other is ADORecordSet class, used the ASP of people see these two categories will understand its meaning,

  ADOConnection refers to a class of database connections, while ADORecordSet referring to execute a query by the ADOConnection returned DataSet class, you can query relevant information ADODB

  Manual class.

  3.The basic functions:

  About ADOConnection categories related methods are:

  1.Connect: database connection method, we introduced the top.For mysql there PConnect, and just like in PHP Language

  2.Execute ($ sql): execute a query returns a result ADORecordSet class.

  3.GetOne ($ sql): Returns the first field of the first row

  4.GetAll ($ sql): returns all data.But this function is of great use, remember to be displayed on the page do not remember when I wrote about the news list input in the previous tutorial

  News made a list of two-dimensional array?Is this statement:

  ================================================== ===================================

  while ($ db-> next_record ())


  $ Array [] = array ( "NewsID" => $ db-> f ( "iNewsID"),

  "NewsTitle" => csubstr ($ db-> f ( "vcNewsTitle"), 0, 20));


  ================================================== ===================================

  This line is what does it mean?Example is the news generated table to be displayed

  $ Array [0] = array ( "NewsID" => 1, "the first press here" "NewsTitle" =>);

  $ Array [1] = array ( "NewsID" => 2, "where the second news" "NewsTitle" =>);


  This form, but if we do not need to control the title, we will be blessed in ADODB, we can write:

  ================================================== ================================

  $ StrQuery = "select iNews, vcNewsTitle from tb_news_ch";

  $ Array = & $ conn-> GetAll ($ strQuery); // Note that this statement

  $ Smarty-> assign ( "News_CH", $ array);

  unset ($ array);

  ================================================== ================================

  Of course, here''s $ conn should be initialized, and I wonder if we did not see to understand?I had to manually create a two-dimensional data used here GetAll directly on the line!!!It is also

  What some people will say ADODB + Smarty is one of the reasons the unbeatable combination of.

  4.SelectLimit ($ sql, $ numrows = -1, $ offset = -1, $ inputarrr = false): Returns a data set, we also not difficult to see from the statement that it is a limited query language

  Sentence, and mysql statement limit a similar effect to a simple example:

  $ Rs = $ conn-> SelectLimit ( "select iNewsID, vcNewsTitle from tb_news_CH", 5, 1);

  do you understand?Saved $ rs is five records in the database starting from the first record.We know that in oracle database does not support the use of limit in SQL statements, but if we make

  Use ADODB, then this problem can be easily solved more!

  5.Close (): Close the database, although PHP will automatically shut down at the end of a page, but in order to complete the program we still want to close the database at the end of the page.

  About ADORecordSet.ADORecordSet is $ conn- results Execute ($ sql) returns>, its basic function is as follows:

  1. Fields ($ colname): Returns the value of the field.

  2. RecordCount (): the number of records contained in.The recorded data set to determine the total number of records.

  3. GetMenu ($ name, [$ default_str = ''''], [$ blank1stItem = true], [$ multiple_select = false], [$ size = 0], [$ moreAttr = '''']) a very good

  Functions, which can be used to return a name = $ name drop-down menu (or box)!!!Of course, it is a string of HTML, this is an exciting goodies, $ name refers to the

  option name attribute, $ default_str is selected by default string, $ blank1stItem pointed out that the first is empty, $ multiple_select Specifies whether check boxes, and we get this

  After a string can use $ smarty -> ( "TemplateVar", "GetMenuStr") to enter a drop-down list (or more than the first frame) in "TemplateVar" at the template

  4. The MoveNext (): a code point of view:

  ================================================== =======

  $ 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?MS ADODB like a thing in that!

  5. MoveFirst (), MoveLast (), Move ($ to): the same, look at the function name you can know that it is what it means.

  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);



Recommend article

Relate article