use-sqlite.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <!-- Always force latest IE rendering engine or request Chrome Frame -->
  6. <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  8. <!-- Use title if it's in the page YAML frontmatter -->
  9. <title>Use SQLite and PHP</title>
  10. <link href="/dashboard/stylesheets/normalize.css" rel="stylesheet" type="text/css" /><link href="/dashboard/stylesheets/all.css" rel="stylesheet" type="text/css" />
  11. <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/3.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  12. <script src="/dashboard/javascripts/modernizr.js" type="text/javascript"></script>
  13. <link href="/dashboard/images/favicon.png" rel="icon" type="image/png" />
  14. </head>
  15. <body class="docs docs_use-sqlite">
  16. <div id="fb-root"></div>
  17. <script>(function(d, s, id) {
  18. var js, fjs = d.getElementsByTagName(s)[0];
  19. if (d.getElementById(id)) return;
  20. js = d.createElement(s); js.id = id;
  21. js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=277385395761685";
  22. fjs.parentNode.insertBefore(js, fjs);
  23. }(document, 'script', 'facebook-jssdk'));</script>
  24. <div class="contain-to-grid">
  25. <nav class="top-bar" data-topbar>
  26. <ul class="title-area">
  27. <li class="name">
  28. <h1><a href="/dashboard/index.html">Apache Friends</a></h1>
  29. </li>
  30. <li class="toggle-topbar menu-icon">
  31. <a href="#">
  32. <span>Menu</span>
  33. </a>
  34. </li>
  35. </ul>
  36. <section class="top-bar-section">
  37. <!-- Right Nav Section -->
  38. <ul class="right">
  39. <li class=""><a href="/applications.html">Applications</a></li>
  40. <li class=""><a href="/dashboard/faq.html">FAQs</a></li>
  41. <li class="active"><a href="/dashboard/howto.html">HOW-TO Guides</a></li>
  42. <li class=""><a target="_blank" href="/dashboard/phpinfo.php">PHPInfo</a></li>
  43. <li class=""><a href="/phpmyadmin/">phpMyAdmin</a></li>
  44. </ul>
  45. </section>
  46. </nav>
  47. </div>
  48. <div id="wrapper">
  49. <div class="hero">
  50. <div class="row">
  51. <div class="large-12 columns">
  52. <h1>Documentation</h1>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="row">
  57. <div class="large-12 columns">
  58. <ul class="sub-nav">
  59. <li>
  60. <a class="pdf" target="_blank" href="/dashboard/docs/use-sqlite.pdf"> Download PDF
  61. <span>use-sqlite.pdf</span>
  62. </a> </li>
  63. </ul>
  64. <article class="asciidoctor">
  65. <h1>Use SQLite and PHP</h1>
  66. <div class="paragraph">
  67. <p>XAMPP comes with built-in support for SQLite, making it easy to get started building database-powered applications with PHP. This guide will walk you through the process of creating and populating a new SQLite database using XAMPP, then accessing the data within it using PHP.</p>
  68. </div>
  69. <div class="paragraph">
  70. <p>Begin by creating and populating a new database using the SQLite command-line client, as below:</p>
  71. </div>
  72. <div class="olist arabic">
  73. <ol class="arabic">
  74. <li>
  75. <p>Open your Windows command prompt by clicking the “Shell” button in the XAMPP control panel.</p>
  76. </li>
  77. <li>
  78. <p>Change to the <em>htdocs\</em> subdirectory of your XAMPP installation directory (typically <em>C:\xampp</em>) and create a new SQLite database file named <em>mydb.sq3</em> with the SQLite command-line client:</p>
  79. <div class="literalblock">
  80. <div class="content">
  81. <pre>cd C:\xampp\htdocs
  82. sqlite3 mydb.sq3</pre>
  83. </div>
  84. </div>
  85. <div class="paragraph">
  86. <p>You should now see an SQLite prompt, as below:</p>
  87. </div>
  88. <div class="imageblock">
  89. <div class="content">
  90. <img src="./images/use-sqlite/image1.png" alt="image1">
  91. </div>
  92. </div>
  93. </li>
  94. <li>
  95. <p>At the <em>sqlite&gt;</em> prompt, create a new table to hold your data. In this example, the table is named items and it contains 3 columns, for item ID, item name and item cost. You can use standard CREATE TABLE syntax to create the table, and you can <a href="http://www.sqlite.org/datatype3.html">learn more about SQLite&#8217;s built-in datatypes here</a>.</p>
  96. <div class="literalblock">
  97. <div class="content">
  98. <pre>CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, price REAL);</pre>
  99. </div>
  100. </div>
  101. </li>
  102. <li>
  103. <p>Add some data to the new inventory table using INSERT commands, as shown below:</p>
  104. <div class="literalblock">
  105. <div class="content">
  106. <pre>INSERT INTO items VALUES ('1001', 'Salt', 3.15);
  107. INSERT INTO items VALUES ('1002', 'Pepper', 2.75);
  108. INSERT INTO items VALUES ('1003', 'Eggs', 2.00);
  109. INSERT INTO items VALUES ('1004', 'Bacon', 7.25);
  110. INSERT INTO items VALUES ('1005', 'Milk', 1.15);
  111. INSERT INTO items VALUES ('1006', 'Strawberries', 8.73);
  112. INSERT INTO items VALUES ('1007', 'Cereal', 2.65);</pre>
  113. </div>
  114. </div>
  115. </li>
  116. <li>
  117. <p>You can now also run a SELECT query on the data. For example, the query below returns all items that cost less than $3:</p>
  118. <div class="literalblock">
  119. <div class="content">
  120. <pre>SELECT * FROM items WHERE price &lt; 3.00;</pre>
  121. </div>
  122. </div>
  123. <div class="imageblock">
  124. <div class="content">
  125. <img src="./images/use-sqlite/image2.png" alt="image2">
  126. </div>
  127. </div>
  128. </li>
  129. <li>
  130. <p>Once you&#8217;re done using the database, exit it by typing <em>.quit</em> at the <em>sqlite&gt;</em> prompt.</p>
  131. </li>
  132. </ol>
  133. </div>
  134. <div class="admonitionblock important">
  135. <table>
  136. <tr>
  137. <td class="icon">
  138. <i class="fa icon-important" title="Important"></i>
  139. </td>
  140. <td class="content">
  141. The database file (in this example, <em>mydb.sq3</em>) contains all your tables and data, so you should remember to back it up regularly.
  142. </td>
  143. </tr>
  144. </table>
  145. </div>
  146. <div class="paragraph">
  147. <p>The previous steps discussed how to create and use an SQLite database using the command-line client. However, more often than not, you&#8217;ll be using an SQLite database in combination with a PHP-powered Web application. XAMPP includes the PHP SQLite extension, so doing this is not very difficult at all.</p>
  148. </div>
  149. <div class="paragraph">
  150. <p>To connect to your SQLite database and execute queries on it with PHP, use your text editor to create an example script named <em>sqlite.php</em> in the <em>htdocs</em> subdirectory of your XAMPP installation directory and fill it with the following code:</p>
  151. </div>
  152. <div class="literalblock">
  153. <div class="content">
  154. <pre>&lt;?php
  155. $db = new SQLite3('mydb.sq3');
  156. $sql = "SELECT * FROM items WHERE price &lt; 3.00";
  157. $result = $db-&gt;query($sql);
  158. while ($row = $result-&gt;fetchArray(SQLITE3_ASSOC)){
  159. echo $row['name'] . ': $' . $row['price'] . '&lt;br/&gt;';
  160. }
  161. unset($db);</pre>
  162. </div>
  163. </div>
  164. <div class="paragraph">
  165. <p>The first line of code creates a new SQLite3 object, using the <em>mydb.sq3</em> database file you created earlier. Then, the object&#8217;s <em>query()</em> method is used to execute a SELECT query on the database, and the result object&#8217;s <em>fetchArray()</em> method is used to iterate over the result set. Adding the SQLITE3_ASSOC parameter to the <em>fetchArray()</em> method tells PHP to return the results as an associative array, making it easy to access individual fields of the result set and display them on a Web page.</p>
  166. </div>
  167. <div class="paragraph">
  168. <p>Once done, save your changes and ensure that your Apache server is running. Then, browse to <a href="http://localhost/sqlite.php" class="bare">http://localhost/sqlite.php</a> to execute the script. You should see something like this:</p>
  169. </div>
  170. <div class="imageblock">
  171. <div class="content">
  172. <img src="./images/use-sqlite/image3.png" alt="image3">
  173. </div>
  174. </div>
  175. <div class="admonitionblock tip">
  176. <table>
  177. <tr>
  178. <td class="icon">
  179. <i class="fa icon-tip" title="Tip"></i>
  180. </td>
  181. <td class="content">
  182. To find out more about SQLite’s powerful features, <a href="http://sqlite.org/docs/">read the SQLite documentation</a>.
  183. </td>
  184. </tr>
  185. </table>
  186. </div>
  187. </article>
  188. </div>
  189. </div>
  190. </div>
  191. <footer>
  192. <div class="row">
  193. <div class="large-12 columns">
  194. <div class="row">
  195. <div class="large-8 columns">
  196. <ul class="social">
  197. <li class="twitter"><a href="https://twitter.com/apachefriends">Follow us on Twitter</a></li>
  198. <li class="facebook"><a href="https://www.facebook.com/we.are.xampp">Like us on Facebook</a></li>
  199. <li class="google"><a href="https://plus.google.com/+xampp/posts">Add us to your G+ Circles</a></li>
  200. </ul>
  201. <ul class="inline-list">
  202. <li><a href="https://www.apachefriends.org/blog.html">Blog</a></li>
  203. <li><a href="https://www.apachefriends.org/privacy_policy.html">Privacy Policy</a></li>
  204. <li>
  205. <a target="_blank" href="http://www.fastly.com/"> CDN provided by
  206. <img width="48" data-2x="/dashboard/images/fastly-logo@2x.png" src="/dashboard/images/fastly-logo.png" />
  207. </a> </li>
  208. </ul>
  209. </div>
  210. <div class="large-4 columns">
  211. <p class="text-right">Copyright (c) 2017, Apache Friends</p>
  212. </div>
  213. </div>
  214. </div>
  215. </div>
  216. </footer>
  217. <!-- JS Libraries -->
  218. <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  219. <script src="/dashboard/javascripts/all.js" type="text/javascript"></script>
  220. </body>
  221. </html>