ezSQL学习

Aug 13, 2007 by 大龙

很不错的数据库操作类,wp的数据库操作类就是从ezSQL修改而来。
下载:download ezsql.zip

下载: readme.php
  1. =======================================================================
  2. AuthorJustin Vincent (justin@visunet.ie)
  3. Web:      http://php.justinvincent.com
  4. Name:      ezSQL
  5. Desc:      Class to make it very easy to deal with database connections.
  6. License: FREE / Donation (LGPL - You may do what you like with ezSQL - no exceptions.)
  7. =======================================================================
  8.  
  9. !! IMPORTANT !!
  10.  
  11. Please send me a mail telling me what you think of ezSQL
  12. and what your using it for!! Cheers. [ justin@visunet.ie ]
  13.  
  14.     If ezSQL has been helpful to you why not make a donation!?
  15.  
  16.         Link -> https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0
  17.  
  18. Also, you should check http://php.justinvincent.com from time
  19. to time as I will be adding new PHP widgets and also a forum
  20. to support those widgets.
  21.  
  22. =======================================================================
  23.  
  24. Change Log:
  25.  
  26. 2.02 - Please note, this chnage log is no longer being used
  27.        please see change_log.htm for changes later than
  28.        2.02
  29.  
  30. 2.01 - Added Disk Caching & Multiple DB connection support
  31.  
  32. 2.00 - Re-factored ezSQL for Oracle, mySQL & SQLite
  33.  
  34.         - DB Object is no longer initialised by default
  35.           (makes it easier to use multiple connections)
  36.  
  37.         - Core ezSQL functions have been separated from DB
  38.           specific functions (makes it easier to add new databases)
  39.  
  40.         - Errors are being piped through trigger_error
  41.           (using standard PHP error logging)
  42.  
  43.         - Abstracted error messages (enabling future translation)
  44.  
  45.         - Upgraded $db->query error return functionality
  46.  
  47.         - Added $db->systdate function to abstract mySQL NOW()
  48.           and Oracle SYSDATE
  49.  
  50.         Note: For other DB Types please use version 1.26
  51.  
  52. 1.26 - Update (All)
  53.  
  54.         - Fixed the pesky regular expression that tests for
  55.           an insert/update etc. Now it works even for the most
  56.           weirdly formatted queries! (thx dille)
  57.  
  58. 1.25 - Update (mySQL/Oracle)
  59.  
  60.         - Optimised $db->query function in both mySQL and Oracle versions.
  61.           Now the return value is working 'as expected' in ALL cases so you
  62.           are always safe using:
  63.  
  64.           if ( $db->query("some query") )
  65.  
  66.           No matter if an insert or a select.
  67.  
  68.           In the case of insert/update the return value is based on the
  69.           number of rows affected (used to be insert id which is
  70.           not valid for an update)
  71.  
  72.           In the case of select the return value is based on number of
  73.           rows returned.
  74.  
  75.           Thx Bill Bruggemeyer :)
  76.  
  77. 1.24 - Update (Docs)
  78.  
  79.         - Now includes tutorial for using EZ Results with Smarty
  80.           templating engine - thx Steve Warwick
  81.  
  82. 1.23 - Update (All PHP versions)
  83.  
  84.         - Fixed the age old problem of returning false on
  85.           successful insert. $db->query()now returns the insert_id
  86.           if there was a successful insert or false if not. Sorry
  87.           that this took so long to fix!
  88.  
  89.           Version Affected: mySQL/Porgress/ms-sql/sqlite
  90.  
  91.         - Added new variable $db->debug_all
  92.  
  93.           By default it is set to false but if you change it
  94.           to true. i.e.
  95.  
  96.           include_once "ez_sql.php";
  97.           $db->debug_all = true;
  98.  
  99.           Then it will print out each and every query and all
  100.           of the results that your script creates.
  101.  
  102.           Very useful if you want to follow the entire logic
  103.           path of what ALL your queries are doing, but can't
  104.           be bothered to put $db->debug() statements all over
  105.           the place!
  106.  
  107.         Update (Postgress SQL Version)
  108.  
  109.          - Our old friend Tom De Bruyne as updated the postgress
  110.            version.
  111.  
  112.            1) It now works without throwing errors (also thanks Geert Nijpels)
  113.  
  114.            2) It now, in theory, returns $this->insert_id after an insert.
  115.  
  116.  
  117. 1.22 - Update (All PHP versions)
  118.  
  119.           - Added new variable $db->num_queries it keeps track
  120.             of exactly how many 'real' (not cached) queries were
  121.             executed (using ezSQL) during the lifetime of one script.
  122.             Useful for debugging and optimizing.
  123.  
  124.           - Put a white table behind the vardump output so that
  125.             it doesn't get lost on dark websites.
  126.  
  127. 1.21 - Update (All Versions)
  128.  
  129.         - Now 'replace' really does return an insert id..
  130.           (the 1.19 fix did not complete the job. Doh!)
  131.  
  132. 1.20 - Update (New Version)
  133.  
  134.         - C++ SQLite version added. Look at ez_demo.cpp.
  135.           (thanks Brennan Falkner)
  136.  
  137. 1.19 - Update (All Versions)
  138.  
  139.         - Fixed bug where any string containing the word 'insert',
  140.           'delete' or 'update' (where those words were not the actual
  141.           query) was causing unexpected results (thx Simon Willison).
  142.  
  143.           The fix was to alter the regular expression to only match
  144.           queries containing those words at the beginning of the query
  145.           (with optional whitespace allowed before the words)
  146.  
  147.           i.e.
  148.  
  149.           THIS:    preg_match("/$word /",strtolower($query))
  150.           TO THIS: preg_match("/^\\s*$word /",strtolower($query))
  151.  
  152.           - Added new sql word 'replace' to the above match pattern
  153.             so that the $db->insert_id would be also be populated
  154.             on 'replace' queries (thx Rolf Dahl)
  155.  
  156. 1.18 - Update (All Versions)
  157.  
  158.         - Added new SQLite version (thanks Brennan Falkner)
  159.  
  160.         - Fixed new bug that was introduced with bug fix 1.14
  161.           false was being returned on successful insert update etc.
  162.           now it is true
  163.  
  164. 1.17 - Update (All Versions)
  165.  
  166.         - New MS-SQL version added (thanks to Tom De Bruyne)
  167.         - Made the donation request 'less annoying' by making it more subtle!
  168.  
  169. 1.16 - Update (All Versions)
  170.  
  171.         - Added new function $db->escape()
  172.           Formats a string correctly to stop accidental
  173.           mal formed queries under all PHP conditions
  174.  
  175. 1.15 - Bug fixes
  176.  
  177.         - (Postgress)
  178.           $this->result = false; was in the wrong place.
  179.           Fixed! Thanks (Carlos CamiGarc)
  180.  
  181.         - (all versions)
  182.           Pesky get_var was still returning null instead of 0 in
  183.           certain cases. Bug fix of !== suggested by Osman
  184.  
  185. 1.14 - Bug fixes
  186.  
  187.         - (all versions)
  188.           Added !='' into the conditional return of get_var.
  189.           because if the result was the number 0 it was not returning anything
  190.  
  191.         - (mySQL / Interbase / Postgress)
  192.           Added $this->result = false; if insert / update / delete
  193.           because it was causing mysql retrieval errors that no one
  194.           knew about due to the @ signs.
  195.  
  196. 1.13 - Update (All Versions)
  197.  
  198.         - Swapped 2nd and 3rd argument order.
  199.         - From.. get_row(query, int row offset, output type)
  200.         - To.... get_row(query, output type, int row offset)
  201.  
  202. 1.12 - Update (All Versions)
  203.  
  204.         - Tweaked the new hide/show error code
  205.         - Made sure the $this->show_errors was always initialised
  206.         - $db->query() function now returns false if there was an SQL error.
  207.           So that you can now do the following when you hide errors.
  208.  
  209.           if ( $db->query("BAD SYNTAX") )
  210.           {
  211.               echo "Bad Query";
  212.           }
  213.  
  214. 1.11 - Update (All Versions)
  215.  
  216.         - added $db->hide_errors();
  217.         - added $db->show_errors();
  218.         - added global array $EZSQL_ERROR;
  219.  
  220. 1.10 - Fix (mySQL)
  221.  
  222.         - Insist that mysql_insert_id(); uses $this->dbh.
  223.  
  224. 1.09 - Bug Fix
  225.  
  226.         - Oracle version had the wrong number of parameters in the
  227.           $db = new db(etc,etc,etc,etc) part.
  228.  
  229.         - Also added var $vardump_called; to all versions.
  230.  
  231. 1.08 - Bug Fix
  232.  
  233.         - Michael fixed the select function in PostgreSQL version.
  234.  
  235. 1.07 - Bug Fix
  236.  
  237.         - Added var $debug_called; to all versions.
  238.  
  239. 1.06 - Update
  240.  
  241.         - Fixed Bug In Oracle Version where an insert was
  242.           causing an error with OCIFetch
  243.         - New PostgreSQL Version Added by Michael Paesold (mpaesold@gmx.at)
  244.  
  245. 1.05 - Bug Fix (mySQL)
  246.  
  247.         - Removed repeated piece of code.
  248.  
  249. 1.04 - Update
  250.  
  251.         - $db->num_rows - variable added (All Versions)
  252.         - $db->rows_affected - variable added ( mySQL / Oracle )
  253.         - New InterBase/FireBase Version Added by LLCedar (llceder@wxs.nl)
  254.  
  255. 1.03 - Update (All Versions)
  256.  
  257.     Enhancements to vardump..
  258.  
  259.         - Added display variable type
  260.         - If no value display No Value / False
  261.         - Added this readme file
  262.  
  263. 1.02 - Update (mySQL version)
  264.  
  265.     Added $db->insert_id to
  266.  
  267. 1.01 - New Version
  268.  
  269.     Oracle 8 Version as below
  270.  
  271. 1.00 - Initial Release
  272.  
  273.     Functions..
  274.  
  275.         - $db->get_results - get multiple row result set from the database (or previously cached results)
  276.         - $db->get_row -- get one row from the database (or previously cached results)
  277.         - $db->get_col - get one column from query (or previously cached results) based on column offset
  278.         - $db->get_var -- get one variable, from one row, from the database (or previously cached results)
  279.         - $db->query -- send a query to the database (and if any results, cache them)
  280.         - $db->debug - print last sql query and returned results (if any)
  281.         - $db->vardump - print the contents and structure of any variable
  282.         - $db->select -- select a new database to work with
  283.         - $db->get_col_info - get information about one or all columns such as column name or type
  284.         - $db = new db -- Initiate new db object.
  285.  
  286. =======================================================================
  287. =======================================================================
  288. AuthorJustin Vincent (justin@visunet.ie)
  289. Web:      http://php.justinvincent.com
  290. Name:      ezSQL
  291. Desc:      Class to make it very easy to deal with database connections.
  292. License: FREE / Donation (LGPL - You may do what you like with ezSQL - no exceptions.)
  293. =======================================================================
  294.  
  295. !! IMPORTANT !!
  296.  
  297. Please send me a mail telling me what you think of ezSQL
  298. and what your using it for!! Cheers. [ justin@visunet.ie ]
  299.  
  300.     If ezSQL has been helpful to you why not make a donation!?
  301.  
  302.         Link -> https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0
  303.  
  304. Also, you should check http://php.justinvincent.com from time
  305. to time as I will be adding new PHP widgets and also a forum
  306. to support those widgets.
  307.  
  308. =======================================================================
  309.  
  310. Change Log:
  311.  
  312. 1.25 - Update (mySQL/Oracle)
  313.  
  314.           - Optimised $db->query function in both mySQL and Oracle versions.
  315.             Now the return value is working 'as expected' in ALL cases so you
  316.             are always safe using:
  317.  
  318.             if ( $db->query("some query") )
  319.  
  320.             No matter if an insert or a select.
  321.  
  322.             In the case of insert/update the return value is based on the
  323.             number of rows affected (used to be insert id which is
  324.             not valid for an update)
  325.  
  326.             In the case of select the return value is based on number of
  327.             rows returned.
  328.  
  329.             Thx Bill Bruggemeyer :)
  330.  
  331. 1.24 - Update (Docs)
  332.  
  333.           - Now includes tutorial for using EZ Results with Smarty
  334.             templating engine - thx Steve Warwick
  335.  
  336. 1.23 - Update (All PHP versions)
  337.  
  338.            - Fixed the age old problem of returning false on
  339.              successful insert. $db->query()now returns the insert_id
  340.              if there was a successful insert or false if not. Sorry