Zend_Db_Adapter.xml 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.adapter">
  4. <title>Zend_Db_Adapter</title>
  5. <para>
  6. <classname>Zend_Db</classname> and its related classes provide a simple SQL database
  7. interface for Zend Framework. The <classname>Zend_Db_Adapter</classname> is the basic class
  8. you use to connect your PHP application to an RDBMS. There is a different Adapter
  9. class for each brand of RDBMS.
  10. </para>
  11. <para>
  12. The <classname>Zend_Db</classname> adapters create a bridge from the vendor-specific PHP
  13. extensions to a common interface to help you write PHP applications
  14. once and deploy with multiple brands of RDBMS with very little effort.
  15. </para>
  16. <para>
  17. The interface of the adapter class is similar to the interface of the
  18. <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink> extension.
  19. <classname>Zend_Db</classname> provides Adapter classes to PDO drivers for the following
  20. RDBMS brands:
  21. </para>
  22. <itemizedlist>
  23. <listitem>
  24. <para>
  25. IBM DB2 and Informix Dynamic Server (IDS), using the
  26. <ulink url="http://www.php.net/pdo-ibm">pdo_ibm</ulink> PHP extension
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. MySQL, using the <ulink url="http://www.php.net/pdo-mysql">pdo_mysql</ulink> PHP
  32. extension
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>
  37. Microsoft SQL Server, using the <ulink
  38. url="http://www.php.net/pdo-mssql">pdo_mssql</ulink> PHP extension
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. Oracle, using the <ulink url="http://www.php.net/pdo-oci">pdo_oci</ulink> PHP
  44. extension
  45. </para>
  46. </listitem>
  47. <listitem>
  48. <para>
  49. PostgreSQL, using the <ulink url="http://www.php.net/pdo-pgsql">pdo_pgsql</ulink>
  50. PHP extension
  51. </para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. SQLite, using the <ulink url="http://www.php.net/pdo-sqlite">pdo_sqlite</ulink> PHP
  56. extension
  57. </para>
  58. </listitem>
  59. </itemizedlist>
  60. <para>
  61. In addition, <classname>Zend_Db</classname> provides Adapter classes that utilize PHP
  62. database extensions for the following RDBMS brands:
  63. </para>
  64. <itemizedlist>
  65. <listitem>
  66. <para>
  67. MySQL, using the <ulink url="http://www.php.net/mysqli">mysqli</ulink> PHP extension
  68. </para>
  69. </listitem>
  70. <listitem>
  71. <para>
  72. Oracle, using the <ulink url="http://www.php.net/oci8">oci8</ulink> PHP extension
  73. </para>
  74. </listitem>
  75. <listitem>
  76. <para>
  77. IBM DB2 and DB2/i5, using the <ulink
  78. url="http://www.php.net/ibm_db2">ibm_db2</ulink> PHP extension
  79. </para>
  80. </listitem>
  81. <listitem>
  82. <para>
  83. Firebird/Interbase, using the <ulink
  84. url="http://www.php.net/ibase">php_interbase</ulink> PHP extension
  85. </para>
  86. </listitem>
  87. </itemizedlist>
  88. <note>
  89. <para>
  90. Each <classname>Zend_Db</classname> Adapter uses a PHP extension. You must have the
  91. respective PHP extension enabled in your PHP environment to use a
  92. <classname>Zend_Db</classname> Adapter. For example, if you use any of the PDO
  93. <classname>Zend_Db</classname> Adapters, you need to enable both the PDO extension and
  94. the PDO driver for the brand of RDBMS you use.
  95. </para>
  96. </note>
  97. <sect2 id="zend.db.adapter.connecting">
  98. <title>Connecting to a Database Using an Adapter</title>
  99. <para>
  100. This section describes how to create an instance of a database Adapter.
  101. This corresponds to making a connection to your RDBMS server from your
  102. PHP application.
  103. </para>
  104. <sect3 id="zend.db.adapter.connecting.constructor">
  105. <title>Using a Zend_Db Adapter Constructor</title>
  106. <para>
  107. You can create an instance of an adapter using its constructor.
  108. An adapter constructor takes one argument, which is an array
  109. of parameters used to declare the connection.
  110. </para>
  111. <example id="zend.db.adapter.connecting.constructor.example">
  112. <title>Using an Adapter Constructor</title>
  113. <programlisting language="php"><![CDATA[
  114. $db = new Zend_Db_Adapter_Pdo_Mysql(array(
  115. 'host' => '127.0.0.1',
  116. 'username' => 'webuser',
  117. 'password' => 'xxxxxxxx',
  118. 'dbname' => 'test'
  119. ));
  120. ]]></programlisting>
  121. </example>
  122. </sect3>
  123. <sect3 id="zend.db.adapter.connecting.factory">
  124. <title>Using the Zend_Db Factory</title>
  125. <para>
  126. As an alternative to using an adapter constructor directly, you
  127. can create an instance of an adapter using the static method
  128. <classname>Zend_Db::factory()</classname>. This method dynamically loads
  129. the adapter class file on demand using
  130. <link linkend="zend.loader.load.class">Zend_Loader::loadClass()</link>.
  131. </para>
  132. <para>
  133. The first argument is a string that names the base name of the
  134. adapter class. For example the string 'Pdo_Mysql' corresponds
  135. to the class Zend_Db_Adapter_Pdo_Mysql. The second argument is
  136. the same array of parameters you would have given to the
  137. adapter constructor.
  138. </para>
  139. <example id="zend.db.adapter.connecting.factory.example">
  140. <title>Using the Adapter Factory Method</title>
  141. <programlisting language="php"><![CDATA[
  142. // We don't need the following statement because the
  143. // Zend_Db_Adapter_Pdo_Mysql file will be loaded for us by the Zend_Db
  144. // factory method.
  145. // require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  146. // Automatically load class Zend_Db_Adapter_Pdo_Mysql
  147. // and create an instance of it.
  148. $db = Zend_Db::factory('Pdo_Mysql', array(
  149. 'host' => '127.0.0.1',
  150. 'username' => 'webuser',
  151. 'password' => 'xxxxxxxx',
  152. 'dbname' => 'test'
  153. ));
  154. ]]></programlisting>
  155. </example>
  156. <para>
  157. If you create your own class that extends
  158. <classname>Zend_Db_Adapter_Abstract_Adapter</classname>, but you do not name your
  159. class with the "Zend_Db_Adapter" package prefix, you can use the
  160. <code>factory()</code> method to load your adapter if you
  161. specify the leading portion of the adapter class with the
  162. 'adapterNamespace' key in the parameters array.
  163. </para>
  164. <example id="zend.db.adapter.connecting.factory.example2">
  165. <title>Using the Adapter Factory Method for a Custom Adapter Class</title>
  166. <programlisting language="php"><![CDATA[
  167. // We don't need to load the adapter class file
  168. // because it will be loaded for us by the Zend_Db factory method.
  169. // Automatically load class MyProject_Db_Adapter_Pdo_Mysql and create
  170. // an instance of it.
  171. $db = Zend_Db::factory('Pdo_Mysql', array(
  172. 'host' => '127.0.0.1',
  173. 'username' => 'webuser',
  174. 'password' => 'xxxxxxxx',
  175. 'dbname' => 'test',
  176. 'adapterNamespace' => 'MyProject_Db_Adapter'
  177. ));
  178. ]]></programlisting>
  179. </example>
  180. </sect3>
  181. <sect3 id="zend.db.adapter.connecting.factory-config">
  182. <title>Using Zend_Config with the Zend_Db Factory</title>
  183. <para>
  184. Optionally, you may specify either argument of the
  185. <code>factory()</code> method as an object of type
  186. <link linkend="zend.config">Zend_Config</link>.
  187. </para>
  188. <para>
  189. If the first argument is a config object, it is expected to
  190. contain a property named <code>adapter</code>, containing the
  191. string naming the adapter class name base. Optionally, the object
  192. may contain a property named <code>params</code>, with
  193. subproperties corresponding to adapter parameter names.
  194. This is used only if the second argument of the
  195. <code>factory()</code> method is absent.
  196. </para>
  197. <example id="zend.db.adapter.connecting.factory.example1">
  198. <title>Using the Adapter Factory Method with a Zend_Config Object</title>
  199. <para>
  200. In the example below, a <classname>Zend_Config</classname> object is created
  201. from an array. You can also load data from an external file using classes such
  202. as <link linkend="zend.config.adapters.ini">Zend_Config_Ini</link>
  203. and <link linkend="zend.config.adapters.xml">Zend_Config_Xml</link>.
  204. </para>
  205. <programlisting language="php"><![CDATA[
  206. $config = new Zend_Config(
  207. array(
  208. 'database' => array(
  209. 'adapter' => 'Mysqli',
  210. 'params' => array(
  211. 'host' => '127.0.0.1',
  212. 'dbname' => 'test',
  213. 'username' => 'webuser',
  214. 'password' => 'secret',
  215. )
  216. )
  217. )
  218. );
  219. $db = Zend_Db::factory($config->database);
  220. ]]></programlisting>
  221. </example>
  222. <para>
  223. The second argument of the <code>factory()</code> method may be
  224. an associative array containing entries corresponding to
  225. adapter parameters. This argument is optional. If the first
  226. argument is of type <classname>Zend_Config</classname>, it is assumed to contain all
  227. parameters, and the second argument is ignored.
  228. </para>
  229. </sect3>
  230. <sect3 id="zend.db.adapter.connecting.parameters">
  231. <title>Adapter Parameters</title>
  232. <para>
  233. The following list explains common parameters recognized by
  234. <classname>Zend_Db</classname> Adapter classes.
  235. </para>
  236. <itemizedlist>
  237. <listitem>
  238. <para>
  239. <emphasis>host</emphasis>:
  240. a string containing a hostname or IP address of the
  241. database server. If the database is running on the
  242. same host as the PHP application, you may use
  243. 'localhost' or '127.0.0.1'.
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <emphasis>username</emphasis>:
  249. account identifier for authenticating a connection to the
  250. RDBMS server.
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <emphasis>password</emphasis>:
  256. account password credential for authenticating a
  257. connection to the RDBMS server.
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <emphasis>dbname</emphasis>:
  263. database instance name on the RDBMS server.
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <emphasis>port</emphasis>:
  269. some RDBMS servers can accept network connections on a
  270. administrator-specified port number. The port
  271. parameter allow you to specify the port to which your
  272. PHP application connects, to match the port configured
  273. on the RDBMS server.
  274. </para>
  275. </listitem>
  276. <listitem>
  277. <para>
  278. <emphasis>options</emphasis>:
  279. this parameter is an associative array of options
  280. that are generic to all <classname>Zend_Db_Adapter</classname> classes.
  281. </para>
  282. </listitem>
  283. <listitem>
  284. <para>
  285. <emphasis>driver_options</emphasis>:
  286. this parameter is an associative array of additional
  287. options that are specific to a given database
  288. extension. One typical use of this parameter is to
  289. set attributes of a PDO driver.
  290. </para>
  291. </listitem>
  292. <listitem>
  293. <para>
  294. <emphasis>adapterNamespace</emphasis>:
  295. names the initial part of the class name for the
  296. adapter, instead of 'Zend_Db_Adapter'. Use this if
  297. you need to use the <code>factory()</code> method to
  298. load a non-Zend database adapter class.
  299. </para>
  300. </listitem>
  301. </itemizedlist>
  302. <example id="zend.db.adapter.connecting.parameters.example1">
  303. <title>Passing the Case-Folding Option to the Factory</title>
  304. <para>
  305. You can specify this option by the constant
  306. <classname>Zend_Db::CASE_FOLDING</classname>.
  307. This corresponds to the <code>ATTR_CASE</code> attribute in
  308. PDO and IBM DB2 database drivers, adjusting the case of
  309. string keys in query result sets. The option takes values
  310. <classname>Zend_Db::CASE_NATURAL</classname> (the default),
  311. <classname>Zend_Db::CASE_UPPER</classname>, and
  312. <classname>Zend_Db::CASE_LOWER</classname>.
  313. </para>
  314. <programlisting language="php"><![CDATA[
  315. $options = array(
  316. Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER
  317. );
  318. $params = array(
  319. 'host' => '127.0.0.1',
  320. 'username' => 'webuser',
  321. 'password' => 'xxxxxxxx',
  322. 'dbname' => 'test',
  323. 'options' => $options
  324. );
  325. $db = Zend_Db::factory('Db2', $params);
  326. ]]></programlisting>
  327. </example>
  328. <example id="zend.db.adapter.connecting.parameters.example2">
  329. <title>Passing the Auto-Quoting Option to the Factory</title>
  330. <para>
  331. You can specify this option by the constant
  332. <classname>Zend_Db::AUTO_QUOTE_IDENTIFIERS</classname>. If the value
  333. is <constant>TRUE</constant> (the default), identifiers like table
  334. names, column names, and even aliases are delimited in all
  335. SQL syntax generated by the Adapter object. This makes it
  336. simple to use identifiers that contain SQL keywords, or
  337. special characters. If the value is <constant>FALSE</constant>,
  338. identifiers are not delimited automatically. If you need
  339. to delimit identifiers, you must do so yourself using the
  340. <code>quoteIdentifier()</code> method.
  341. </para>
  342. <programlisting language="php"><![CDATA[
  343. $options = array(
  344. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  345. );
  346. $params = array(
  347. 'host' => '127.0.0.1',
  348. 'username' => 'webuser',
  349. 'password' => 'xxxxxxxx',
  350. 'dbname' => 'test',
  351. 'options' => $options
  352. );
  353. $db = Zend_Db::factory('Pdo_Mysql', $params);
  354. ]]></programlisting>
  355. </example>
  356. <example id="zend.db.adapter.connecting.parameters.example3">
  357. <title>Passing PDO Driver Options to the Factory</title>
  358. <programlisting language="php"><![CDATA[
  359. $pdoParams = array(
  360. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
  361. );
  362. $params = array(
  363. 'host' => '127.0.0.1',
  364. 'username' => 'webuser',
  365. 'password' => 'xxxxxxxx',
  366. 'dbname' => 'test',
  367. 'driver_options' => $pdoParams
  368. );
  369. $db = Zend_Db::factory('Pdo_Mysql', $params);
  370. echo $db->getConnection()
  371. ->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
  372. ]]></programlisting>
  373. </example>
  374. <example id="zend.db.adapter.connecting.parameters.example4">
  375. <title>Passing Serialization Options to the Factory</title>
  376. <programlisting language="php"><![CDATA[
  377. $options = array(
  378. Zend_Db::ALLOW_SERIALIZATION => false
  379. );
  380. $params = array(
  381. 'host' => '127.0.0.1',
  382. 'username' => 'webuser',
  383. 'password' => 'xxxxxxxx',
  384. 'dbname' => 'test',
  385. 'options' => $options
  386. );
  387. $db = Zend_Db::factory('Pdo_Mysql', $params);
  388. ]]></programlisting>
  389. </example>
  390. </sect3>
  391. <sect3 id="zend.db.adapter.connecting.getconnection">
  392. <title>Managing Lazy Connections</title>
  393. <para>
  394. Creating an instance of an Adapter class does not immediately
  395. connect to the RDBMS server. The Adapter saves the connection
  396. parameters, and makes the actual connection on demand, the
  397. first time you need to execute a query. This ensures that
  398. creating an Adapter object is quick and inexpensive. You can
  399. create an instance of an Adapter even if you are not certain
  400. that you need to run any database queries during the current
  401. request your application is serving.
  402. </para>
  403. <para>
  404. If you need to force the Adapter to connect to the RDBMS, use
  405. the <code>getConnection()</code> method. This method returns
  406. an object for the connection as represented by the respective
  407. PHP database extension. For example, if you use any of the
  408. Adapter classes for PDO drivers, then
  409. <code>getConnection()</code> returns the PDO object, after
  410. initiating it as a live connection to the specific database.
  411. </para>
  412. <para>
  413. It can be useful to force the connection if you want to catch
  414. any exceptions it throws as a result of invalid account
  415. credentials, or other failure to connect to the RDBMS server.
  416. These exceptions are not thrown until the connection is made,
  417. so it can help simplify your application code if you handle the
  418. exceptions in one place, instead of at the time of
  419. the first query against the database.
  420. </para>
  421. <para>
  422. Additionally, an adapter can get serialized to store it, for example,
  423. in a session variable. This can be very useful not only for the
  424. adapter itself, but for other objects that aggregate it, like a
  425. <classname>Zend_Db_Select</classname> object. By default, adapters are allowed
  426. to be serialized, if you don't want it, you should consider passing the
  427. <classname>Zend_Db::ALLOW_SERIALIZATION=false</classname> option, see the example
  428. above. To respect lazy connections principle, the adapter won't reconnect itself
  429. after being unserialized. You must then call <code>getConnection()
  430. </code> yourself. You can make the adapter auto-reconnect by passing the
  431. <classname>Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE=true</classname> as an adapter
  432. option.
  433. </para>
  434. <example id="zend.db.adapter.connecting.getconnection.example">
  435. <title>Handling Connection Exceptions</title>
  436. <programlisting language="php"><![CDATA[
  437. try {
  438. $db = Zend_Db::factory('Pdo_Mysql', $parameters);
  439. $db->getConnection();
  440. } catch (Zend_Db_Adapter_Exception $e) {
  441. // perhaps a failed login credential, or perhaps the RDBMS is not running
  442. } catch (Zend_Exception $e) {
  443. // perhaps factory() failed to load the specified Adapter class
  444. }
  445. ]]></programlisting>
  446. </example>
  447. </sect3>
  448. </sect2>
  449. <sect2 id="zend.db.adapter.example-database">
  450. <title>Example Database</title>
  451. <para>
  452. In the documentation for <classname>Zend_Db</classname> classes, we use a set of simple
  453. tables to illustrate usage of the classes and methods. These
  454. example tables could store information for tracking bugs in a
  455. software development project. The database contains four tables:
  456. </para>
  457. <itemizedlist>
  458. <listitem>
  459. <para>
  460. <emphasis>accounts</emphasis> stores
  461. information about each user of the bug-tracking database.
  462. </para>
  463. </listitem>
  464. <listitem>
  465. <para>
  466. <emphasis>products</emphasis> stores
  467. information about each product for which a bug can be
  468. logged.
  469. </para>
  470. </listitem>
  471. <listitem>
  472. <para>
  473. <emphasis>bugs</emphasis> stores information
  474. about bugs, including that current state of the bug, the
  475. person who reported the bug, the person who is assigned to
  476. fix the bug, and the person who is assigned to verify the
  477. fix.
  478. </para>
  479. </listitem>
  480. <listitem>
  481. <para>
  482. <emphasis>bugs_products</emphasis> stores a
  483. relationship between bugs and products. This implements a
  484. many-to-many relationship, because a given bug may be
  485. relevant to multiple products, and of course a given
  486. product can have multiple bugs.
  487. </para>
  488. </listitem>
  489. </itemizedlist>
  490. <para>
  491. The following SQL data definition language pseudocode describes the
  492. tables in this example database. These example tables are used
  493. extensively by the automated unit tests for <classname>Zend_Db</classname>.
  494. </para>
  495. <programlisting language="sql"><![CDATA[
  496. CREATE TABLE accounts (
  497. account_name VARCHAR(100) NOT NULL PRIMARY KEY
  498. );
  499. CREATE TABLE products (
  500. product_id INTEGER NOT NULL PRIMARY KEY,
  501. product_name VARCHAR(100)
  502. );
  503. CREATE TABLE bugs (
  504. bug_id INTEGER NOT NULL PRIMARY KEY,
  505. bug_description VARCHAR(100),
  506. bug_status VARCHAR(20),
  507. reported_by VARCHAR(100) REFERENCES accounts(account_name),
  508. assigned_to VARCHAR(100) REFERENCES accounts(account_name),
  509. verified_by VARCHAR(100) REFERENCES accounts(account_name)
  510. );
  511. CREATE TABLE bugs_products (
  512. bug_id INTEGER NOT NULL REFERENCES bugs,
  513. product_id INTEGER NOT NULL REFERENCES products,
  514. PRIMARY KEY (bug_id, product_id)
  515. );
  516. ]]></programlisting>
  517. <para>
  518. Also notice that the <code>bugs</code> table contains multiple
  519. foreign key references to the <code>accounts</code> table.
  520. Each of these foreign keys may reference a different row in the
  521. <code>accounts</code> table for a given bug.
  522. </para>
  523. <para>
  524. The diagram below illustrates the physical data model of the
  525. example database.
  526. </para>
  527. <para>
  528. <inlinegraphic width="387" scale="100" align="center" valign="middle"
  529. fileref="figures/zend.db.adapter.example-database.png" format="PNG" />
  530. </para>
  531. </sect2>
  532. <sect2 id="zend.db.adapter.select">
  533. <title>Reading Query Results</title>
  534. <para>
  535. This section describes methods of the Adapter class with which you
  536. can run SELECT queries and retrieve the query results.
  537. </para>
  538. <sect3 id="zend.db.adapter.select.fetchall">
  539. <title>Fetching a Complete Result Set</title>
  540. <para>
  541. You can run a SQL SELECT query and retrieve its results in one
  542. step using the <code>fetchAll()</code> method.
  543. </para>
  544. <para>
  545. The first argument to this method is a string containing a
  546. SELECT statement. Alternatively, the first argument can be an
  547. object of class <link linkend="zend.db.select">Zend_Db_Select</link>.
  548. The Adapter automatically converts this object to a string
  549. representation of the SELECT statement.
  550. </para>
  551. <para>
  552. The second argument to <code>fetchAll()</code> is an array of
  553. values to substitute for parameter placeholders in the SQL
  554. statement.
  555. </para>
  556. <example id="zend.db.adapter.select.fetchall.example">
  557. <title>Using fetchAll()</title>
  558. <programlisting language="php"><![CDATA[
  559. $sql = 'SELECT * FROM bugs WHERE bug_id = ?';
  560. $result = $db->fetchAll($sql, 2);
  561. ]]></programlisting>
  562. </example>
  563. </sect3>
  564. <sect3 id="zend.db.adapter.select.fetch-mode">
  565. <title>Changing the Fetch Mode</title>
  566. <para>
  567. By default, <code>fetchAll()</code> returns an array of
  568. rows, each of which is an associative array. The keys of the
  569. associative array are the columns or column aliases named in
  570. the select query.
  571. </para>
  572. <para>
  573. You can specify a different style of fetching results using the
  574. <code>setFetchMode()</code> method. The modes supported are
  575. identified by constants:
  576. </para>
  577. <itemizedlist>
  578. <listitem>
  579. <para>
  580. <emphasis>Zend_Db::FETCH_ASSOC</emphasis>:
  581. return data in an array of associative arrays.
  582. The array keys are column names, as strings. This is the default fetch mode
  583. for <classname>Zend_Db_Adapter</classname> classes.
  584. </para>
  585. <para>
  586. Note that if your select-list contains more than one
  587. column with the same name, for example if they are from
  588. two different tables in a JOIN, there can be only one
  589. entry in the associative array for a given name.
  590. If you use the FETCH_ASSOC mode, you should specify
  591. column aliases in your SELECT query to ensure that the
  592. names result in unique array keys.
  593. </para>
  594. <para>
  595. By default, these strings are returned as they are
  596. returned by the database driver. This is typically the
  597. spelling of the column in the RDBMS server. You can
  598. specify the case for these strings, using the
  599. <classname>Zend_Db::CASE_FOLDING</classname> option.
  600. Specify this when instantiating the Adapter.
  601. See <xref linkend="zend.db.adapter.connecting.parameters.example1" />.
  602. </para>
  603. </listitem>
  604. <listitem>
  605. <para>
  606. <emphasis>Zend_Db::FETCH_NUM</emphasis>:
  607. return data in an array of arrays. The arrays are
  608. indexed by integers, corresponding to the position of
  609. the respective field in the select-list of the query.
  610. </para>
  611. </listitem>
  612. <listitem>
  613. <para>
  614. <emphasis>Zend_Db::FETCH_BOTH</emphasis>:
  615. return data in an array of arrays. The array keys are
  616. both strings as used in the FETCH_ASSOC mode, and
  617. integers as used in the FETCH_NUM mode. Note that the
  618. number of elements in the array is double that which
  619. would be in the array if you used either FETCH_ASSOC
  620. or FETCH_NUM.
  621. </para>
  622. </listitem>
  623. <listitem>
  624. <para>
  625. <emphasis>Zend_Db::FETCH_COLUMN</emphasis>:
  626. return data in an array of values. The value in each array
  627. is the value returned by one column of the result set.
  628. By default, this is the first column, indexed by 0.
  629. </para>
  630. </listitem>
  631. <listitem>
  632. <para>
  633. <emphasis>Zend_Db::FETCH_OBJ</emphasis>:
  634. return data in an array of objects. The default class
  635. is the PHP built-in class stdClass. Columns of the
  636. result set are available as public properties of the
  637. object.
  638. </para>
  639. </listitem>
  640. </itemizedlist>
  641. <example id="zend.db.adapter.select.fetch-mode.example">
  642. <title>Using setFetchMode()</title>
  643. <programlisting language="php"><![CDATA[
  644. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  645. $result = $db->fetchAll('SELECT * FROM bugs WHERE bug_id = ?', 2);
  646. // $result is an array of objects
  647. echo $result[0]->bug_description;
  648. ]]></programlisting>
  649. </example>
  650. </sect3>
  651. <sect3 id="zend.db.adapter.select.fetchassoc">
  652. <title>Fetching a Result Set as an Associative Array</title>
  653. <para>
  654. The <code>fetchAssoc()</code> method returns data in an array
  655. of associative arrays, regardless of what value you have set
  656. for the fetch mode.
  657. </para>
  658. <example id="zend.db.adapter.select.fetchassoc.example">
  659. <title>Using fetchAssoc()</title>
  660. <programlisting language="php"><![CDATA[
  661. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  662. $result = $db->fetchAssoc('SELECT * FROM bugs WHERE bug_id = ?', 2);
  663. // $result is an array of associative arrays, in spite of the fetch mode
  664. echo $result[0]['bug_description'];
  665. ]]></programlisting>
  666. </example>
  667. </sect3>
  668. <sect3 id="zend.db.adapter.select.fetchcol">
  669. <title>Fetching a Single Column from a Result Set</title>
  670. <para>
  671. The <code>fetchCol()</code> method returns data in an array
  672. of values, regardless of the value you have set for the fetch mode.
  673. This only returns the first column returned by the query.
  674. Any other columns returned by the query are discarded.
  675. If you need to return a column other than the first, see
  676. <xref linkend="zend.db.statement.fetching.fetchcolumn" />.
  677. </para>
  678. <example id="zend.db.adapter.select.fetchcol.example">
  679. <title>Using fetchCol()</title>
  680. <programlisting language="php"><![CDATA[
  681. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  682. $result = $db->fetchCol(
  683. 'SELECT bug_description, bug_id FROM bugs WHERE bug_id = ?', 2);
  684. // contains bug_description; bug_id is not returned
  685. echo $result[0];
  686. ]]></programlisting>
  687. </example>
  688. </sect3>
  689. <sect3 id="zend.db.adapter.select.fetchpairs">
  690. <title>Fetching Key-Value Pairs from a Result Set</title>
  691. <para>
  692. The <code>fetchPairs()</code> method returns data in an array
  693. of key-value pairs, as an associative array with a single entry
  694. per row. The key of this associative array is taken from the
  695. first column returned by the SELECT query. The value is taken
  696. from the second column returned by the SELECT query. Any other
  697. columns returned by the query are discarded.
  698. </para>
  699. <para>
  700. You should design the SELECT query so that the first column
  701. returned has unique values. If there are duplicates values in
  702. the first column, entries in the associative array will be
  703. overwritten.
  704. </para>
  705. <example id="zend.db.adapter.select.fetchpairs.example">
  706. <title>Using fetchPairs()</title>
  707. <programlisting language="php"><![CDATA[
  708. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  709. $result = $db->fetchPairs('SELECT bug_id, bug_status FROM bugs');
  710. echo $result[2];
  711. ]]></programlisting>
  712. </example>
  713. </sect3>
  714. <sect3 id="zend.db.adapter.select.fetchrow">
  715. <title>Fetching a Single Row from a Result Set</title>
  716. <para>
  717. The <code>fetchRow()</code> method returns data using the
  718. current fetch mode, but it returns only the first row
  719. fetched from the result set.
  720. </para>
  721. <example id="zend.db.adapter.select.fetchrow.example">
  722. <title>Using fetchRow()</title>
  723. <programlisting language="php"><![CDATA[
  724. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  725. $result = $db->fetchRow('SELECT * FROM bugs WHERE bug_id = 2');
  726. // note that $result is a single object, not an array of objects
  727. echo $result->bug_description;
  728. ]]></programlisting>
  729. </example>
  730. </sect3>
  731. <sect3 id="zend.db.adapter.select.fetchone">
  732. <title>Fetching a Single Scalar from a Result Set</title>
  733. <para>
  734. The <code>fetchOne()</code> method is like a combination
  735. of <code>fetchRow()</code> with <code>fetchCol()</code>,
  736. in that it returns data only for the first row fetched from
  737. the result set, and it returns only the value of the first
  738. column in that row. Therefore it returns only a single
  739. scalar value, not an array or an object.
  740. </para>
  741. <example id="zend.db.adapter.select.fetchone.example">
  742. <title>Using fetchOne()</title>
  743. <programlisting language="php"><![CDATA[
  744. $result = $db->fetchOne('SELECT bug_status FROM bugs WHERE bug_id = 2');
  745. // this is a single string value
  746. echo $result;
  747. ]]></programlisting>
  748. </example>
  749. </sect3>
  750. </sect2>
  751. <sect2 id="zend.db.adapter.write">
  752. <title>Writing Changes to the Database</title>
  753. <para>
  754. You can use the Adapter class to write new data or change existing
  755. data in your database. This section describes methods to do these
  756. operations.
  757. </para>
  758. <sect3 id="zend.db.adapter.write.insert">
  759. <title>Inserting Data</title>
  760. <para>
  761. You can add new rows to a table in your database using the
  762. <code>insert()</code> method. The first argument is a string
  763. that names the table, and the second argument is an associative
  764. array, mapping column names to data values.
  765. </para>
  766. <example id="zend.db.adapter.write.insert.example">
  767. <title>Inserting in a Table</title>
  768. <programlisting language="php"><![CDATA[
  769. $data = array(
  770. 'created_on' => '2007-03-22',
  771. 'bug_description' => 'Something wrong',
  772. 'bug_status' => 'NEW'
  773. );
  774. $db->insert('bugs', $data);
  775. ]]></programlisting>
  776. </example>
  777. <para>
  778. Columns you exclude from the array of data are not specified to
  779. the database. Therefore, they follow the same rules that an
  780. SQL INSERT statement follows: if the column has a DEFAULT
  781. clause, the column takes that value in the row created,
  782. otherwise the column is left in a NULL state.
  783. </para>
  784. <para>
  785. By default, the values in your data array are inserted using
  786. parameters. This reduces risk of some types of security
  787. issues. You don't need to apply escaping or quoting to values
  788. in the data array.
  789. </para>
  790. <para>
  791. You might need values in the data array to be treated as SQL
  792. expressions, in which case they should not be quoted. By
  793. default, all data values passed as strings are treated as
  794. string literals. To specify that the value is an SQL
  795. expression and therefore should not be quoted, pass the value
  796. in the data array as an object of type <classname>Zend_Db_Expr</classname> instead
  797. of a plain string.
  798. </para>
  799. <example id="zend.db.adapter.write.insert.example2">
  800. <title>Inserting Expressions in a Table</title>
  801. <programlisting language="php"><![CDATA[
  802. $data = array(
  803. 'created_on' => new Zend_Db_Expr('CURDATE()'),
  804. 'bug_description' => 'Something wrong',
  805. 'bug_status' => 'NEW'
  806. );
  807. $db->insert('bugs', $data);
  808. ]]></programlisting>
  809. </example>
  810. </sect3>
  811. <sect3 id="zend.db.adapter.write.lastinsertid">
  812. <title>Retrieving a Generated Value</title>
  813. <para>
  814. Some RDBMS brands support auto-incrementing primary keys.
  815. A table defined this way generates a primary key value
  816. automatically during an INSERT of a new row. The return value
  817. of the <code>insert()</code> method is <emphasis>not</emphasis>
  818. the last inserted ID, because the table might not have an
  819. auto-incremented column. Instead, the return value is the
  820. number of rows affected (usually 1).
  821. </para>
  822. <para>
  823. If your table is defined with an auto-incrementing primary key,
  824. you can call the <code>lastInsertId()</code> method after the
  825. insert. This method returns the last value generated in the
  826. scope of the current database connection.
  827. </para>
  828. <example id="zend.db.adapter.write.lastinsertid.example-1">
  829. <title>Using lastInsertId() for an Auto-Increment Key</title>
  830. <programlisting language="php"><![CDATA[
  831. $db->insert('bugs', $data);
  832. // return the last value generated by an auto-increment column
  833. $id = $db->lastInsertId();
  834. ]]></programlisting>
  835. </example>
  836. <para>
  837. Some RDBMS brands support a sequence object, which generates
  838. unique values to serve as primary key values. To support
  839. sequences, the <code>lastInsertId()</code> method accepts two
  840. optional string arguments. These arguments name the table and
  841. the column, assuming you have followed the convention that a
  842. sequence is named using the table and column names for which
  843. the sequence generates values, and a suffix "_seq". This is
  844. based on the convention used by PostgreSQL when naming
  845. sequences for SERIAL columns. For example, a table "bugs" with
  846. primary key column "bug_id" would use a sequence named
  847. "bugs_bug_id_seq".
  848. </para>
  849. <example id="zend.db.adapter.write.lastinsertid.example-2">
  850. <title>Using lastInsertId() for a Sequence</title>
  851. <programlisting language="php"><![CDATA[
  852. $db->insert('bugs', $data);
  853. // return the last value generated by sequence 'bugs_bug_id_seq'.
  854. $id = $db->lastInsertId('bugs', 'bug_id');
  855. // alternatively, return the last value generated by sequence 'bugs_seq'.
  856. $id = $db->lastInsertId('bugs');
  857. ]]></programlisting>
  858. </example>
  859. <para>
  860. If the name of your sequence object does not follow this naming
  861. convention, use the <code>lastSequenceId()</code> method
  862. instead. This method takes a single string argument, naming
  863. the sequence literally.
  864. </para>
  865. <example id="zend.db.adapter.write.lastinsertid.example-3">
  866. <title>Using lastSequenceId()</title>
  867. <programlisting language="php"><![CDATA[
  868. $db->insert('bugs', $data);
  869. // return the last value generated by sequence 'bugs_id_gen'.
  870. $id = $db->lastSequenceId('bugs_id_gen');
  871. ]]></programlisting>
  872. </example>
  873. <para>
  874. For RDBMS brands that don't support sequences, including MySQL,
  875. Microsoft SQL Server, and SQLite, the arguments to the
  876. lastInsertId() method are ignored, and the value returned is the
  877. most recent value generated for any table by INSERT operations
  878. during the current connection. For these RDBMS brands, the
  879. lastSequenceId() method always returns <constant>NULL</constant>.
  880. </para>
  881. <note>
  882. <title>Why Not Use "SELECT MAX(id) FROM table"?</title>
  883. <para>
  884. Sometimes this query returns the most recent primary key
  885. value inserted into the table. However, this technique
  886. is not safe to use in an environment where multiple clients are
  887. inserting records to the database. It is possible, and
  888. therefore is bound to happen eventually, that another
  889. client inserts another row in the instant between the
  890. insert performed by your client application and your query
  891. for the MAX(id) value. Thus the value returned does not
  892. identify the row you inserted, it identifies the row
  893. inserted by some other client. There is no way to know
  894. when this has happened.
  895. </para>
  896. <para>
  897. Using a strong transaction isolation mode such as
  898. "repeatable read" can mitigate this risk, but some RDBMS
  899. brands don't support the transaction isolation required for
  900. this, or else your application may use a lower transaction
  901. isolation mode by design.
  902. </para>
  903. <para>
  904. Furthermore, using an expression like "MAX(id)+1" to generate
  905. a new value for a primary key is not safe, because two clients
  906. could do this query simultaneously, and then both use the same
  907. calculated value for their next INSERT operation.
  908. </para>
  909. <para>
  910. All RDBMS brands provide mechanisms to generate unique
  911. values, and to return the last value generated. These
  912. mechanisms necessarily work outside of the scope of
  913. transaction isolation, so there is no chance of two clients
  914. generating the same value, and there is no chance that the
  915. value generated by another client could be reported to your
  916. client's connection as the last value generated.
  917. </para>
  918. </note>
  919. </sect3>
  920. <sect3 id="zend.db.adapter.write.update">
  921. <title>Updating Data</title>
  922. <para>
  923. You can update rows in a database table using the
  924. <code>update()</code> method of an Adapter. This method takes
  925. three arguments: the first is the name of the table; the
  926. second is an associative array mapping columns to change to new
  927. values to assign to these columns.
  928. </para>
  929. <para>
  930. The values in the data array are treated as string literals.
  931. See <xref linkend="zend.db.adapter.write.insert" />
  932. for information on using SQL expressions in the data array.
  933. </para>
  934. <para>
  935. The third argument is a string containing an SQL expression
  936. that is used as criteria for the rows to change. The values
  937. and identifiers in this argument are not quoted or escaped.
  938. You are responsible for ensuring that any dynamic content is
  939. interpolated into this string safely.
  940. See <xref linkend="zend.db.adapter.quoting" />
  941. for methods to help you do this.
  942. </para>
  943. <para>
  944. The return value is the number of rows affected by the update
  945. operation.
  946. </para>
  947. <example id="zend.db.adapter.write.update.example">
  948. <title>Updating Rows</title>
  949. <programlisting language="php"><![CDATA[
  950. $data = array(
  951. 'updated_on' => '2007-03-23',
  952. 'bug_status' => 'FIXED'
  953. );
  954. $n = $db->update('bugs', $data, 'bug_id = 2');
  955. ]]></programlisting>
  956. </example>
  957. <para>
  958. If you omit the third argument, then all rows in the database
  959. table are updated with the values specified in the data array.
  960. </para>
  961. <para>
  962. If you provide an array of strings as the third argument, these
  963. strings are joined together as terms in an expression separated
  964. by <code>AND</code> operators.
  965. </para>
  966. <example id="zend.db.adapter.write.update.example-array">
  967. <title>Updating Rows Using an Array of Expressions</title>
  968. <programlisting language="php"><![CDATA[
  969. $data = array(
  970. 'updated_on' => '2007-03-23',
  971. 'bug_status' => 'FIXED'
  972. );
  973. $where[] = "reported_by = 'goofy'";
  974. $where[] = "bug_status = 'OPEN'";
  975. $n = $db->update('bugs', $data, $where);
  976. // Resulting SQL is:
  977. // UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
  978. // WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
  979. ]]></programlisting>
  980. </example>
  981. </sect3>
  982. <sect3 id="zend.db.adapter.write.delete">
  983. <title>Deleting Data</title>
  984. <para>
  985. You can delete rows from a database table using the
  986. <code>delete()</code> method. This method takes two arguments:
  987. the first is a string naming the table.
  988. </para>
  989. <para>
  990. The second argument is a string containing an SQL expression
  991. that is used as criteria for the rows to delete. The values
  992. and identifiers in this argument are not quoted or escaped.
  993. You are responsible for ensuring that any dynamic content is
  994. interpolated into this string safely.
  995. See <xref linkend="zend.db.adapter.quoting" />
  996. for methods to help you do this.
  997. </para>
  998. <para>
  999. The return value is the number of rows affected by the delete
  1000. operation.
  1001. </para>
  1002. <example id="zend.db.adapter.write.delete.example">
  1003. <title>Deleting Rows</title>
  1004. <programlisting language="php"><![CDATA[
  1005. $n = $db->delete('bugs', 'bug_id = 3');
  1006. ]]></programlisting>
  1007. </example>
  1008. <para>
  1009. If you omit the second argument, the result is that all rows in
  1010. the database table are deleted.
  1011. </para>
  1012. <para>
  1013. If you provide an array of strings as the second argument, these
  1014. strings are joined together as terms in an expression separated
  1015. by <code>AND</code> operators.
  1016. </para>
  1017. </sect3>
  1018. </sect2>
  1019. <sect2 id="zend.db.adapter.quoting">
  1020. <title>Quoting Values and Identifiers</title>
  1021. <para>
  1022. When you form SQL queries, often it is the case that you need to
  1023. include the values of PHP variables in SQL expressions. This is
  1024. risky, because if the value in a PHP string contains certain
  1025. symbols, such as the quote symbol, it could result in invalid SQL.
  1026. For example, notice the imbalanced quote characters in the
  1027. following query:
  1028. <programlisting language="php"><![CDATA[
  1029. $name = "O'Reilly";
  1030. $sql = "SELECT * FROM bugs WHERE reported_by = '$name'";
  1031. echo $sql;
  1032. // SELECT * FROM bugs WHERE reported_by = 'O'Reilly'
  1033. ]]></programlisting>
  1034. </para>
  1035. <para>
  1036. Even worse is the risk that such code mistakes might be exploited
  1037. deliberately by a person who is trying to manipulate the function
  1038. of your web application. If they can specify the value of a PHP
  1039. variable through the use of an HTTP parameter or other mechanism,
  1040. they might be able to make your SQL queries do things that you
  1041. didn't intend them to do, such as return data to which the person
  1042. should not have privilege to read. This is a serious and widespread
  1043. technique for violating application security, known as "SQL Injection" (see <ulink
  1044. url="http://en.wikipedia.org/wiki/SQL_Injection">http://en.wikipedia.org/wiki/SQL_Injection</ulink>).
  1045. </para>
  1046. <para>
  1047. The <classname>Zend_Db</classname> Adapter class provides convenient functions to help
  1048. you reduce vulnerabilities to SQL Injection attacks in your PHP code.
  1049. The solution is to escape special characters such as quotes in PHP
  1050. values before they are interpolated into your SQL strings.
  1051. This protects against both accidental and deliberate manipulation
  1052. of SQL strings by PHP variables that contain special characters.
  1053. </para>
  1054. <sect3 id="zend.db.adapter.quoting.quote">
  1055. <title>Using quote()</title>
  1056. <para>
  1057. The <code>quote()</code> method accepts a single argument, a
  1058. scalar string value. It returns the value with special
  1059. characters escaped in a manner appropriate for the RDBMS you
  1060. are using, and surrounded by string value delimiters. The
  1061. standard SQL string value delimiter is the single-quote
  1062. (<code>'</code>).
  1063. </para>
  1064. <example id="zend.db.adapter.quoting.quote.example">
  1065. <title>Using quote()</title>
  1066. <programlisting language="php"><![CDATA[
  1067. $name = $db->quote("O'Reilly");
  1068. echo $name;
  1069. // 'O\'Reilly'
  1070. $sql = "SELECT * FROM bugs WHERE reported_by = $name";
  1071. echo $sql;
  1072. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1073. ]]></programlisting>
  1074. </example>
  1075. <para>
  1076. Note that the return value of <code>quote()</code> includes the
  1077. quote delimiters around the string. This is different from
  1078. some functions that escape special characters but do not add
  1079. the quote delimiters, for example <ulink
  1080. url="http://www.php.net/mysqli_real_escape_string">mysql_real_escape_string()</ulink>.
  1081. </para>
  1082. <para>
  1083. Values may need to be quoted or not quoted according to the SQL
  1084. datatype context in which they are used. For instance, in some
  1085. RDBMS brands, an integer value must not be quoted as a string
  1086. if it is compared to an integer-type column or expression.
  1087. In other words, the following is an error in some SQL
  1088. implementations, assuming <code>intColumn</code> has a SQL
  1089. datatype of <code>INTEGER</code>
  1090. <programlisting language="php"><![CDATA[
  1091. SELECT * FROM atable WHERE intColumn = '123'
  1092. ]]></programlisting>
  1093. </para>
  1094. <para>
  1095. You can use the optional second argument to the
  1096. <code>quote()</code> method to apply quoting selectively for
  1097. the SQL datatype you specify.
  1098. </para>
  1099. <example id="zend.db.adapter.quoting.quote.example-2">
  1100. <title>Using quote() with a SQL Type</title>
  1101. <programlisting language="php"><![CDATA[
  1102. $value = '1234';
  1103. $sql = 'SELECT * FROM atable WHERE intColumn = '
  1104. . $db->quote($value, 'INTEGER');
  1105. ]]></programlisting>
  1106. </example>
  1107. <para>
  1108. Each <classname>Zend_Db_Adapter</classname> class has encoded the names of numeric
  1109. SQL datatypes for the respective brand of RDBMS. You can also
  1110. use the constants <classname>Zend_Db::INT_TYPE</classname>,
  1111. <classname>Zend_Db::BIGINT_TYPE</classname>, and
  1112. <classname>Zend_Db::FLOAT_TYPE</classname> to write code in a more
  1113. RDBMS-independent way.
  1114. </para>
  1115. <para>
  1116. <classname>Zend_Db_Table</classname> specifies SQL types to <code>quote()</code>
  1117. automatically when generating SQL queries that reference a
  1118. table's key columns.
  1119. </para>
  1120. </sect3>
  1121. <sect3 id="zend.db.adapter.quoting.quote-into">
  1122. <title>Using quoteInto()</title>
  1123. <para>
  1124. The most typical usage of quoting is to interpolate a PHP
  1125. variable into a SQL expression or statement. You can use the
  1126. <code>quoteInto()</code> method to do this in one step. This
  1127. method takes two arguments: the first argument is a string
  1128. containing a placeholder symbol (<code>?</code>), and the
  1129. second argument is a value or PHP variable that should be
  1130. substituted for that placeholder.
  1131. </para>
  1132. <para>
  1133. The placeholder symbol is the same symbol used by many RDBMS
  1134. brands for positional parameters, but the
  1135. <code>quoteInto()</code> method only emulates query parameters.
  1136. The method simply interpolates the value into the string,
  1137. escapes special characters, and applies quotes around it.
  1138. True query parameters maintain the separation between the SQL
  1139. string and the parameters as the statement is parsed in the
  1140. RDBMS server.
  1141. </para>
  1142. <example id="zend.db.adapter.quoting.quote-into.example">
  1143. <title>Using quoteInto()</title>
  1144. <programlisting language="php"><![CDATA[
  1145. $sql = $db->quoteInto("SELECT * FROM bugs WHERE reported_by = ?", "O'Reilly");
  1146. echo $sql;
  1147. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1148. ]]></programlisting>
  1149. </example>
  1150. <para>
  1151. You can use the optional third parameter of
  1152. <code>quoteInto()</code> to specify the SQL datatype. Numeric
  1153. datatypes are not quoted, and other types are quoted.
  1154. </para>
  1155. <example id="zend.db.adapter.quoting.quote-into.example-2">
  1156. <title>Using quoteInto() with a SQL Type</title>
  1157. <programlisting language="php"><![CDATA[
  1158. $sql = $db
  1159. ->quoteInto("SELECT * FROM bugs WHERE bug_id = ?", '1234', 'INTEGER');
  1160. echo $sql;
  1161. // SELECT * FROM bugs WHERE reported_by = 1234
  1162. ]]></programlisting>
  1163. </example>
  1164. </sect3>
  1165. <sect3 id="zend.db.adapter.quoting.quote-identifier">
  1166. <title>Using quoteIdentifier()</title>
  1167. <para>
  1168. Values are not the only part of SQL syntax that might need to
  1169. be variable. If you use PHP variables to name tables, columns,
  1170. or other identifiers in your SQL statements, you might need to
  1171. quote these strings too. By default, SQL identifiers have
  1172. syntax rules like PHP and most other programming languages.
  1173. For example, identifiers should not contain spaces, certain
  1174. punctuation or special characters, or international characters.
  1175. Also certain words are reserved for SQL syntax, and should not
  1176. be used as identifiers.
  1177. </para>
  1178. <para>
  1179. However, SQL has a feature called <emphasis>delimited identifiers</emphasis>,
  1180. which allows broader choices for the spelling of identifiers.
  1181. If you enclose a SQL identifier in the proper types of quotes,
  1182. you can use identifiers with spellings that would be invalid
  1183. without the quotes. Delimited identifiers can contain spaces,
  1184. punctuation, or international characters. You can also use SQL
  1185. reserved words if you enclose them in identifier delimiters.
  1186. </para>
  1187. <para>
  1188. The <code>quoteIdentifier()</code> method works like
  1189. <code>quote()</code>, but it applies the identifier delimiter
  1190. characters to the string according to the type of Adapter you
  1191. use. For example, standard SQL uses double-quotes
  1192. (<code>"</code>) for identifier delimiters, and most RDBMS
  1193. brands use that symbol. MySQL uses back-quotes
  1194. (<code>`</code>) by default. The
  1195. <code>quoteIdentifier()</code> method also escapes special
  1196. characters within the string argument.
  1197. </para>
  1198. <example id="zend.db.adapter.quoting.quote-identifier.example">
  1199. <title>Using quoteIdentifier()</title>
  1200. <programlisting language="php"><![CDATA[
  1201. // we might have a table name that is an SQL reserved word
  1202. $tableName = $db->quoteIdentifier("order");
  1203. $sql = "SELECT * FROM $tableName";
  1204. echo $sql
  1205. // SELECT * FROM "order"
  1206. ]]></programlisting>
  1207. </example>
  1208. <para>
  1209. SQL delimited identifiers are case-sensitive, unlike unquoted
  1210. identifiers. Therefore, if you use delimited identifiers, you
  1211. must use the spelling of the identifier exactly as it is stored
  1212. in your schema, including the case of the letters.
  1213. </para>
  1214. <para>
  1215. In most cases where SQL is generated within <classname>Zend_Db</classname> classes,
  1216. the default is that all identifiers are delimited
  1217. automatically. You can change this behavior with the option
  1218. <classname>Zend_Db::AUTO_QUOTE_IDENTIFIERS</classname>. Specify this
  1219. when instantiating the Adapter.
  1220. See <xref linkend="zend.db.adapter.connecting.parameters.example2" />.
  1221. </para>
  1222. </sect3>
  1223. </sect2>
  1224. <sect2 id="zend.db.adapter.transactions">
  1225. <title>Controlling Database Transactions</title>
  1226. <para>
  1227. Databases define transactions as logical units of work that can be
  1228. committed or rolled back as a single change, even if they operate
  1229. on multiple tables. All queries to a database are executed within
  1230. the context of a transaction, even if the database driver manages
  1231. them implicitly. This is called <emphasis>auto-commit</emphasis>
  1232. mode, in which the database driver creates a transaction for every
  1233. statement you execute, and commits that transaction after your
  1234. SQL statement has been executed. By default, all <classname>Zend_Db</classname> Adapter
  1235. classes operate in auto-commit mode.
  1236. </para>
  1237. <para>
  1238. Alternatively, you can specify the beginning and resolution of a
  1239. transaction, and thus control how many SQL queries are included in
  1240. a single group that is committed (or rolled back) as a single
  1241. operation. Use the <code>beginTransaction()</code> method to
  1242. initiate a transaction. Subsequent SQL statements are executed in
  1243. the context of the same transaction until you resolve it
  1244. explicitly.
  1245. </para>
  1246. <para>
  1247. To resolve the transaction, use either the <code>commit()</code> or
  1248. <code>rollBack()</code> methods. The <code>commit()</code> method
  1249. marks changes made during your transaction as committed, which
  1250. means the effects of these changes are shown in queries run in
  1251. other transactions.
  1252. </para>
  1253. <para>
  1254. The <code>rollBack()</code> method does the opposite: it discards
  1255. the changes made during your transaction. The changes are
  1256. effectively undone, and the state of the data returns to how it was
  1257. before you began your transaction. However, rolling back your
  1258. transaction has no effect on changes made by other transactions
  1259. running concurrently.
  1260. </para>
  1261. <para>
  1262. After you resolve this transaction, <classname>Zend_Db_Adapter</classname>
  1263. returns to auto-commit mode until you call
  1264. <code>beginTransaction()</code> again.
  1265. </para>
  1266. <example id="zend.db.adapter.transactions.example">
  1267. <title>Managing a Transaction to Ensure Consistency</title>
  1268. <programlisting language="php"><![CDATA[
  1269. // Start a transaction explicitly.
  1270. $db->beginTransaction();
  1271. try {
  1272. // Attempt to execute one or more queries:
  1273. $db->query(...);
  1274. $db->query(...);
  1275. $db->query(...);
  1276. // If all succeed, commit the transaction and all changes
  1277. // are committed at once.
  1278. $db->commit();
  1279. } catch (Exception $e) {
  1280. // If any of the queries failed and threw an exception,
  1281. // we want to roll back the whole transaction, reversing
  1282. // changes made in the transaction, even those that succeeded.
  1283. // Thus all changes are committed together, or none are.
  1284. $db->rollBack();
  1285. echo $e->getMessage();
  1286. }
  1287. ]]></programlisting>
  1288. </example>
  1289. </sect2>
  1290. <sect2 id="zend.db.adapter.list-describe">
  1291. <title>Listing and Describing Tables</title>
  1292. <para>
  1293. The <code>listTables()</code> method returns an array of strings,
  1294. naming all tables in the current database.
  1295. </para>
  1296. <para>
  1297. The <code>describeTable()</code> method returns an associative
  1298. array of metadata about a table. Specify the name of the table
  1299. as a string in the first argument to this method. The second
  1300. argument is optional, and names the schema in which the table
  1301. exists.
  1302. </para>
  1303. <para>
  1304. The keys of the associative array returned are the column names of
  1305. the table. The value corresponding to each column is also an
  1306. associative array, with the following keys and values:
  1307. </para>
  1308. <table frame="all" cellpadding="5" id="zend.db.adapter.list-describe.metadata">
  1309. <title>Metadata Fields Returned by describeTable()</title>
  1310. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  1311. <thead>
  1312. <row>
  1313. <entry>Key</entry>
  1314. <entry>Type</entry>
  1315. <entry>Description</entry>
  1316. </row>
  1317. </thead>
  1318. <tbody>
  1319. <row>
  1320. <entry>SCHEMA_NAME</entry>
  1321. <entry>(string)</entry>
  1322. <entry>Name of the database schema in which this table exists.</entry>
  1323. </row>
  1324. <row>
  1325. <entry>TABLE_NAME</entry>
  1326. <entry>(string)</entry>
  1327. <entry>Name of the table to which this column belongs.</entry>
  1328. </row>
  1329. <row>
  1330. <entry>COLUMN_NAME</entry>
  1331. <entry>(string)</entry>
  1332. <entry>Name of the column.</entry>
  1333. </row>
  1334. <row>
  1335. <entry>COLUMN_POSITION</entry>
  1336. <entry>(integer)</entry>
  1337. <entry>Ordinal position of the column in the table.</entry>
  1338. </row>
  1339. <row>
  1340. <entry>DATA_TYPE</entry>
  1341. <entry>(string)</entry>
  1342. <entry>RDBMS name of the datatype of the column.</entry>
  1343. </row>
  1344. <row>
  1345. <entry>DEFAULT</entry>
  1346. <entry>(string)</entry>
  1347. <entry>Default value for the column, if any.</entry>
  1348. </row>
  1349. <row>
  1350. <entry>NULLABLE</entry>
  1351. <entry>(boolean)</entry>
  1352. <entry>
  1353. True if the column accepts SQL NULLs, false if the column has a NOT NULL
  1354. constraint.
  1355. </entry>
  1356. </row>
  1357. <row>
  1358. <entry>LENGTH</entry>
  1359. <entry>(integer)</entry>
  1360. <entry>Length or size of the column as reported by the RDBMS.</entry>
  1361. </row>
  1362. <row>
  1363. <entry>SCALE</entry>
  1364. <entry>(integer)</entry>
  1365. <entry>Scale of SQL NUMERIC or DECIMAL type.</entry>
  1366. </row>
  1367. <row>
  1368. <entry>PRECISION</entry>
  1369. <entry>(integer)</entry>
  1370. <entry>Precision of SQL NUMERIC or DECIMAL type.</entry>
  1371. </row>
  1372. <row>
  1373. <entry>UNSIGNED</entry>
  1374. <entry>(boolean)</entry>
  1375. <entry>True if an integer-based type is reported as UNSIGNED.</entry>
  1376. </row>
  1377. <row>
  1378. <entry>PRIMARY</entry>
  1379. <entry>(boolean)</entry>
  1380. <entry>True if the column is part of the primary key of this table.</entry>
  1381. </row>
  1382. <row>
  1383. <entry>PRIMARY_POSITION</entry>
  1384. <entry>(integer)</entry>
  1385. <entry>Ordinal position (1-based) of the column in the primary key.</entry>
  1386. </row>
  1387. <row>
  1388. <entry>IDENTITY</entry>
  1389. <entry>(boolean)</entry>
  1390. <entry>True if the column uses an auto-generated value.</entry>
  1391. </row>
  1392. </tbody>
  1393. </tgroup>
  1394. </table>
  1395. <note>
  1396. <title>How the IDENTITY Metadata Field Relates to Specific RDBMSs</title>
  1397. <para>
  1398. The IDENTITY metadata field was chosen as an 'idiomatic' term to
  1399. represent a relation to surrogate keys. This field can be
  1400. commonly known by the following values:-
  1401. </para>
  1402. <itemizedlist>
  1403. <listitem>
  1404. <para>
  1405. <code>IDENTITY</code> - DB2, MSSQL
  1406. </para>
  1407. </listitem>
  1408. <listitem>
  1409. <para>
  1410. <code>AUTO_INCREMENT</code> - MySQL
  1411. </para>
  1412. </listitem>
  1413. <listitem>
  1414. <para>
  1415. <code>SERIAL</code> - PostgreSQL
  1416. </para>
  1417. </listitem>
  1418. <listitem>
  1419. <para>
  1420. <code>SEQUENCE</code> - Oracle
  1421. </para>
  1422. </listitem>
  1423. </itemizedlist>
  1424. </note>
  1425. <para>
  1426. If no table exists matching the table name and optional schema name
  1427. specified, then <code>describeTable()</code> returns an empty array.
  1428. </para>
  1429. </sect2>
  1430. <sect2 id="zend.db.adapter.closing">
  1431. <title>Closing a Connection</title>
  1432. <para>
  1433. Normally it is not necessary to close a database connection. PHP
  1434. automatically cleans up all resources and the end of a request.
  1435. Database extensions are designed to close the connection as the
  1436. reference to the resource object is cleaned up.
  1437. </para>
  1438. <para>
  1439. However, if you have a long-duration PHP script that initiates many
  1440. database connections, you might need to close the connection, to avoid
  1441. exhausting the capacity of your RDBMS server. You can use the
  1442. Adapter's <code>closeConnection()</code> method to explicitly close
  1443. the underlying database connection.
  1444. </para>
  1445. <para>
  1446. Since release 1.7.2, you could check you are currently connected to the RDBMS
  1447. server with the method <code>isConnected()</code>. This means that a connection
  1448. resource has been initiated and wasn't closed. This function is not currently
  1449. able to test for example a server side closing of the connection. This is
  1450. internally use to close the connection. It allow you to close the connection
  1451. multiple times without errors. It was already the case before 1.7.2 for PDO
  1452. adapters but not for the others.
  1453. </para>
  1454. <example id="zend.db.adapter.closing.example">
  1455. <title>Closing a Database Connection</title>
  1456. <programlisting language="php"><![CDATA[
  1457. $db->closeConnection();
  1458. ]]></programlisting>
  1459. </example>
  1460. <note>
  1461. <title>Does Zend_Db Support Persistent Connections?</title>
  1462. <para>
  1463. The usage of persistent connections is not supported
  1464. or encouraged in <classname>Zend_Db</classname>.
  1465. </para>
  1466. <para>
  1467. Using persistent connections can cause an excess of idle
  1468. connections on the RDBMS server, which causes more problems
  1469. than any performance gain you might achieve by reducing the
  1470. overhead of making connections.
  1471. </para>
  1472. <para>
  1473. Database connections have state. That is, some objects in the
  1474. RDBMS server exist in session scope. Examples are locks, user
  1475. variables, temporary tables, and information about the most
  1476. recently executed query, such as rows affected, and last
  1477. generated id value. If you use persistent connections, your
  1478. application could access invalid or privileged data that were
  1479. created in a previous PHP request.
  1480. </para>
  1481. </note>
  1482. </sect2>
  1483. <sect2 id="zend.db.adapter.other-statements">
  1484. <title>Running Other Database Statements</title>
  1485. <para>
  1486. There might be cases in which you need to access the connection
  1487. object directly, as provided by the PHP database extension. Some
  1488. of these extensions may offer features that are not surfaced by
  1489. methods of <classname>Zend_Db_Adapter_Abstract</classname>.
  1490. </para>
  1491. <para>
  1492. For example, all SQL statements run by <classname>Zend_Db</classname> are prepared, then
  1493. executed. However, some database features are incompatible with
  1494. prepared statements. DDL statements like CREATE and ALTER cannot
  1495. be prepared in MySQL. Also, SQL statements don't benefit from the <ulink
  1496. url="http://dev.mysql.com/doc/refman/5.1/en/query-cache-how.html">MySQL Query
  1497. Cache</ulink>, prior to MySQL 5.1.17.
  1498. </para>
  1499. <para>
  1500. Most PHP database extensions provide a method to execute SQL
  1501. statements without preparing them. For example, in PDO, this
  1502. method is <code>exec()</code>. You can access the connection
  1503. object in the PHP extension directly using getConnection().
  1504. </para>
  1505. <example id="zend.db.adapter.other-statements.example">
  1506. <title>Running a Non-Prepared Statement in a PDO Adapter</title>
  1507. <programlisting language="php"><![CDATA[
  1508. $result = $db->getConnection()->exec('DROP TABLE bugs');
  1509. ]]></programlisting>
  1510. </example>
  1511. <para>
  1512. Similarly, you can access other methods or properties that are
  1513. specific to PHP database extensions. Be aware, though, that by
  1514. doing this you might constrain your application to the interface
  1515. provided by the extension for a specific brand of RDBMS.
  1516. </para>
  1517. <para>
  1518. In future versions of <classname>Zend_Db</classname>, there will be opportunities to
  1519. add method entry points for functionality that is common to
  1520. the supported PHP database extensions. This will not affect
  1521. backward compatibility.
  1522. </para>
  1523. </sect2>
  1524. <sect2 id="zend.db.adapter.server-version">
  1525. <title>Retrieving Server Version</title>
  1526. <para>
  1527. Since release 1.7.2, you could retrieve the server version in PHP syntax
  1528. style to be able to use <code>version_compare()</code>. If the information
  1529. isn't available, you will receive <constant>NULL</constant>.
  1530. </para>
  1531. <example id="zend.db.adapter.server-version.example">
  1532. <title>Verifying server version before running a query</title>
  1533. <programlisting language="php"><![CDATA[
  1534. $version = $db->getServerVersion();
  1535. if (!is_null($version)) {
  1536. if (version_compare($version, '5.0.0', '>=')) {
  1537. // do something
  1538. } else {
  1539. // do something else
  1540. }
  1541. } else {
  1542. // impossible to read server version
  1543. }
  1544. ]]></programlisting>
  1545. </example>
  1546. </sect2>
  1547. <sect2 id="zend.db.adapter.adapter-notes">
  1548. <title>Notes on Specific Adapters</title>
  1549. <para>
  1550. This section lists differences between the Adapter classes of which
  1551. you should be aware.
  1552. </para>
  1553. <sect3 id="zend.db.adapter.adapter-notes.ibm-db2">
  1554. <title>IBM DB2</title>
  1555. <itemizedlist>
  1556. <listitem>
  1557. <para>
  1558. Specify this Adapter to the factory() method with the
  1559. name 'Db2'.
  1560. </para>
  1561. </listitem>
  1562. <listitem>
  1563. <para>
  1564. This Adapter uses the PHP extension ibm_db2.
  1565. </para>
  1566. </listitem>
  1567. <listitem>
  1568. <para>
  1569. IBM DB2 supports both sequences and auto-incrementing
  1570. keys. Therefore the arguments to
  1571. <code>lastInsertId()</code> are optional. If you give
  1572. no arguments, the Adapter returns the last value
  1573. generated for an auto-increment key. If you give
  1574. arguments, the Adapter returns the last value generated
  1575. by the sequence named according to the convention
  1576. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1577. </para>
  1578. </listitem>
  1579. </itemizedlist>
  1580. </sect3>
  1581. <sect3 id="zend.db.adapter.adapter-notes.mysqli">
  1582. <title>MySQLi</title>
  1583. <itemizedlist>
  1584. <listitem>
  1585. <para>
  1586. Specify this Adapter to the <code>factory()</code>
  1587. method with the name 'Mysqli'.
  1588. </para>
  1589. </listitem>
  1590. <listitem>
  1591. <para>
  1592. This Adapter utilizes the PHP extension mysqli.
  1593. </para>
  1594. </listitem>
  1595. <listitem>
  1596. <para>
  1597. MySQL does not support sequences, so
  1598. <code>lastInsertId()</code> ignores its arguments and
  1599. always returns the last value generated for an
  1600. auto-increment key. The <code>lastSequenceId()</code>
  1601. method returns <constant>NULL</constant>.
  1602. </para>
  1603. </listitem>
  1604. </itemizedlist>
  1605. </sect3>
  1606. <sect3 id="zend.db.adapter.adapter-notes.oracle">
  1607. <title>Oracle</title>
  1608. <itemizedlist>
  1609. <listitem>
  1610. <para>
  1611. Specify this Adapter to the <code>factory()</code>
  1612. method with the name 'Oracle'.
  1613. </para>
  1614. </listitem>
  1615. <listitem>
  1616. <para>
  1617. This Adapter uses the PHP extension oci8.
  1618. </para>
  1619. </listitem>
  1620. <listitem>
  1621. <para>
  1622. Oracle does not support auto-incrementing keys, so you
  1623. should specify the name of a sequence to
  1624. <code>lastInsertId()</code> or
  1625. <code>lastSequenceId()</code>.
  1626. </para>
  1627. </listitem>
  1628. <listitem>
  1629. <para>
  1630. The Oracle extension does not support positional
  1631. parameters. You must use named parameters.
  1632. </para>
  1633. </listitem>
  1634. <listitem>
  1635. <para>
  1636. Currently the <classname>Zend_Db::CASE_FOLDING</classname> option
  1637. is not supported by the Oracle adapter. To use this
  1638. option with Oracle, you must use the PDO OCI adapter.
  1639. </para>
  1640. </listitem>
  1641. <listitem>
  1642. <para>
  1643. By default, LOB fields are returned as OCI-Lob objects. You could
  1644. retrieve them as string for all requests by using driver options
  1645. <code>'lob_as_string'</code> or for particular request by using
  1646. <code>setLobAsString(boolean)</code> on adapter or on statement.
  1647. </para>
  1648. </listitem>
  1649. </itemizedlist>
  1650. </sect3>
  1651. <sect3 id="zend.db.adapter.adapter-notes.pdo-ibm">
  1652. <title>PDO for IBM DB2 and Informix Dynamic Server (IDS)</title>
  1653. <itemizedlist>
  1654. <listitem>
  1655. <para>
  1656. Specify this Adapter to the <code>factory()</code>
  1657. method with the name 'Pdo_Ibm'.
  1658. </para>
  1659. </listitem>
  1660. <listitem>
  1661. <para>
  1662. This Adapter uses the PHP extensions pdo and pdo_ibm.
  1663. </para>
  1664. </listitem>
  1665. <listitem>
  1666. <para>
  1667. You must use at least PDO_IBM extension version 1.2.2.
  1668. If you have an earlier version of this extension, you
  1669. must upgrade the PDO_IBM extension from PECL.
  1670. </para>
  1671. </listitem>
  1672. </itemizedlist>
  1673. </sect3>
  1674. <sect3 id="zend.db.adapter.adapter-notes.pdo-mssql">
  1675. <title>PDO Microsoft SQL Server</title>
  1676. <itemizedlist>
  1677. <listitem>
  1678. <para>
  1679. Specify this Adapter to the <code>factory()</code>
  1680. method with the name 'Pdo_Mssql'.
  1681. </para>
  1682. </listitem>
  1683. <listitem>
  1684. <para>
  1685. This Adapter uses the PHP extensions pdo and pdo_mssql.
  1686. </para>
  1687. </listitem>
  1688. <listitem>
  1689. <para>
  1690. Microsoft SQL Server does not support sequences, so
  1691. <code>lastInsertId()</code> ignores its arguments and
  1692. always returns the last value generated for an
  1693. auto-increment key. The <code>lastSequenceId()</code>
  1694. method returns <constant>NULL</constant>.
  1695. </para>
  1696. </listitem>
  1697. <listitem>
  1698. <para>
  1699. If you are working with unicode strings in an encoding other than UCS-2
  1700. (such as UTF-8), you may have to perform a conversion in your application
  1701. code or store the data in a binary column. Please refer to <ulink
  1702. url="http://support.microsoft.com/kb/232580">Microsoft's Knowledge
  1703. Base</ulink> for more information.
  1704. </para>
  1705. </listitem>
  1706. <listitem>
  1707. <para>
  1708. <classname>Zend_Db_Adapter_Pdo_Mssql</classname> sets
  1709. <code>QUOTED_IDENTIFIER ON</code> immediately after connecting to a SQL
  1710. Server database. This makes the driver use the standard SQL identifier
  1711. delimiter symbol (<code>"</code>) instead of the
  1712. proprietary square-brackets syntax SQL Server uses for
  1713. delimiting identifiers.
  1714. </para>
  1715. </listitem>
  1716. <listitem>
  1717. <para>
  1718. You can specify <code>pdoType</code> as a key in the
  1719. options array. The value can be "mssql" (the default),
  1720. "dblib", "freetds", or "sybase". This option affects
  1721. the DSN prefix the adapter uses when constructing the
  1722. DSN string. Both "freetds" and "sybase" imply a prefix
  1723. of "sybase:", which is used for the
  1724. <ulink url="http://www.freetds.org/">FreeTDS</ulink> set
  1725. of libraries.
  1726. See also
  1727. <ulink url="http://www.php.net/manual/en/ref.pdo-dblib.connection.php">
  1728. http://www.php.net/manual/en/ref.pdo-dblib.connection.php</ulink>
  1729. for more information on the DSN prefixes used in this driver.
  1730. </para>
  1731. </listitem>
  1732. </itemizedlist>
  1733. </sect3>
  1734. <sect3 id="zend.db.adapter.adapter-notes.pdo-mysql">
  1735. <title>PDO MySQL</title>
  1736. <itemizedlist>
  1737. <listitem>
  1738. <para>
  1739. Specify this Adapter to the <code>factory()</code>
  1740. method with the name 'Pdo_Mysql'.
  1741. </para>
  1742. </listitem>
  1743. <listitem>
  1744. <para>
  1745. This Adapter uses the PHP extensions pdo and pdo_mysql.
  1746. </para>
  1747. </listitem>
  1748. <listitem>
  1749. <para>
  1750. MySQL does not support sequences, so
  1751. <code>lastInsertId()</code> ignores its arguments and
  1752. always returns the last value generated for an
  1753. auto-increment key. The <code>lastSequenceId()</code>
  1754. method returns <constant>NULL</constant>.
  1755. </para>
  1756. </listitem>
  1757. </itemizedlist>
  1758. </sect3>
  1759. <sect3 id="zend.db.adapter.adapter-notes.pdo-oci">
  1760. <title>PDO Oracle</title>
  1761. <itemizedlist>
  1762. <listitem>
  1763. <para>
  1764. Specify this Adapter to the <code>factory()</code>
  1765. method with the name 'Pdo_Oci'.
  1766. </para>
  1767. </listitem>
  1768. <listitem>
  1769. <para>
  1770. This Adapter uses the PHP extensions pdo and pdo_oci.
  1771. </para>
  1772. </listitem>
  1773. <listitem>
  1774. <para>
  1775. Oracle does not support auto-incrementing keys, so you
  1776. should specify the name of a sequence to
  1777. <code>lastInsertId()</code> or
  1778. <code>lastSequenceId()</code>.
  1779. </para>
  1780. </listitem>
  1781. </itemizedlist>
  1782. </sect3>
  1783. <sect3 id="zend.db.adapter.adapter-notes.pdo-pgsql">
  1784. <title>PDO PostgreSQL</title>
  1785. <itemizedlist>
  1786. <listitem>
  1787. <para>
  1788. Specify this Adapter to the <code>factory()</code>
  1789. method with the name 'Pdo_Pgsql'.
  1790. </para>
  1791. </listitem>
  1792. <listitem>
  1793. <para>
  1794. This Adapter uses the PHP extensions pdo and pdo_pgsql.
  1795. </para>
  1796. </listitem>
  1797. <listitem>
  1798. <para>
  1799. PostgreSQL supports both sequences and auto-incrementing
  1800. keys. Therefore the arguments to
  1801. <code>lastInsertId()</code> are optional. If you give
  1802. no arguments, the Adapter returns the last value
  1803. generated for an auto-increment key. If you give
  1804. arguments, the Adapter returns the last value generated
  1805. by the sequence named according to the convention
  1806. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1807. </para>
  1808. </listitem>
  1809. </itemizedlist>
  1810. </sect3>
  1811. <sect3 id="zend.db.adapter.adapter-notes.pdo-sqlite">
  1812. <title>PDO SQLite</title>
  1813. <itemizedlist>
  1814. <listitem>
  1815. <para>
  1816. Specify this Adapter to the <code>factory()</code>
  1817. method with the name 'Pdo_Sqlite'.
  1818. </para>
  1819. </listitem>
  1820. <listitem>
  1821. <para>
  1822. This Adapter uses the PHP extensions pdo and pdo_sqlite.
  1823. </para>
  1824. </listitem>
  1825. <listitem>
  1826. <para>
  1827. SQLite does not support sequences, so
  1828. <code>lastInsertId()</code> ignores its arguments and
  1829. always returns the last value generated for an
  1830. auto-increment key. The <code>lastSequenceId()</code>
  1831. method returns <constant>NULL</constant>.
  1832. </para>
  1833. </listitem>
  1834. <listitem>
  1835. <para>
  1836. To connect to an SQLite2 database, specify
  1837. <code>'sqlite2'=>true</code> in the array of
  1838. parameters when creating an instance of the
  1839. Pdo_Sqlite Adapter.
  1840. </para>
  1841. </listitem>
  1842. <listitem>
  1843. <para>
  1844. To connect to an in-memory SQLite database,
  1845. specify <code>'dbname'=>':memory:'</code> in the
  1846. array of parameters when creating an instance of
  1847. the Pdo_Sqlite Adapter.
  1848. </para>
  1849. </listitem>
  1850. <listitem>
  1851. <para>
  1852. Older versions of the SQLite driver for PHP do not seem
  1853. to support the PRAGMA commands necessary to ensure that
  1854. short column names are used in result sets. If you
  1855. have problems that your result sets are returned with
  1856. keys of the form "tablename.columnname" when you do a
  1857. join query, then you should upgrade to the current
  1858. version of PHP.
  1859. </para>
  1860. </listitem>
  1861. </itemizedlist>
  1862. </sect3>
  1863. <sect3 id="zend.db.adapter.adapter-notes.firebird">
  1864. <title>Firebird/Interbase</title>
  1865. <itemizedlist>
  1866. <listitem>
  1867. <para>
  1868. This Adapter uses the PHP extension php_interbase.
  1869. </para>
  1870. </listitem>
  1871. <listitem>
  1872. <para>
  1873. Firebird/interbase does not support auto-incrementing keys,
  1874. so you should specify the name of a sequence to
  1875. <code>lastInsertId()</code> or
  1876. <code>lastSequenceId()</code>.
  1877. </para>
  1878. </listitem>
  1879. <listitem>
  1880. <para>
  1881. Currently the <classname>Zend_Db::CASE_FOLDING</classname> option
  1882. is not supported by the Firebird/interbase adapter.
  1883. Unquoted identifiers are automatically returned in
  1884. upper case.
  1885. </para>
  1886. </listitem>
  1887. <listitem>
  1888. <para>Adapter name is ZendX_Db_Adapter_Firebird.</para>
  1889. <para>
  1890. Remember to use the param adapterNamespace with value ZendX_Db_Adapter.
  1891. </para>
  1892. <para>
  1893. We recommend to update the gds32.dll (or linux equivalent) bundled with php,
  1894. to the same version of the server. For Firebird the equivalent gds32.dll is
  1895. fbclient.dll.
  1896. </para>
  1897. <para>
  1898. By default all identifiers (tables names, fields) are returned in upper
  1899. case.
  1900. </para>
  1901. </listitem>
  1902. </itemizedlist>
  1903. </sect3>
  1904. </sect2>
  1905. </sect1>
  1906. <!--
  1907. vim:se ts=4 sw=4 et:
  1908. -->