@@ -29,10 +29,9 @@ persisted to the database. Writing in flat PHP is quick and dirty:
2929
3030 <?php
3131 // index.php
32- $link = mysql_connect('localhost', 'myuser', 'mypassword');
33- mysql_select_db('blog_db', $link);
32+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
3433
35- $result = mysql_query ('SELECT id, title FROM post', $link );
34+ $result = $link->query ('SELECT id, title FROM post');
3635 ?>
3736
3837 <!DOCTYPE html>
@@ -43,7 +42,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
4342 <body>
4443 <h1>List of Posts</h1>
4544 <ul>
46- <?php while ($row = mysql_fetch_assoc( $result)): ?>
45+ <?php while ($row = $result->fetch(PDO::FETCH_ASSOC )): ?>
4746 <li>
4847 <a href="/show.php?id=<?php echo $row['id'] ?>">
4948 <?php echo $row['title'] ?>
@@ -55,7 +54,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
5554 </html>
5655
5756 <?php
58- mysql_close( $link) ;
57+ $link = null ;
5958 ?>
6059
6160That's quick to write, fast to execute, and, as your app grows, impossible
@@ -81,26 +80,24 @@ Isolating the Presentation
8180~~~~~~~~~~~~~~~~~~~~~~~~~~
8281
8382The code can immediately gain from separating the application "logic" from
84- the code that prepares the HTML "presentation":
85-
86- .. code-block :: html+php
83+ the code that prepares the HTML "presentation"::
8784
8885 // index.php
89- $link = mysql_connect('localhost', 'myuser', 'mypassword');
90- mysql_select_db('blog_db', $link);
86+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
9187
92- $result = mysql_query ('SELECT id, title FROM post', $link );
88+ $result = $link->query ('SELECT id, title FROM post');
9389
9490 $posts = array();
95- while ($row = mysql_fetch_assoc( $result)) {
91+ while ($row = $result->fetch(PDO::FETCH_ASSOC )) {
9692 $posts[] = $row;
9793 }
9894
99- mysql_close( $link) ;
95+ $link = null ;
10096
10197 // include the HTML presentation code
10298 require 'templates/list.php';
10399
100+
104101The HTML code is now stored in a separate file (``templates/list.php ``), which
105102is primarily an HTML file that uses a template-like PHP syntax:
106103
@@ -141,31 +138,29 @@ Isolating the Application (Domain) Logic
141138So far the application contains only one page. But what if a second page
142139needed to use the same database connection, or even the same array of blog
143140posts? Refactor the code so that the core behavior and data-access functions
144- of the application are isolated in a new file called ``model.php ``:
145-
146- .. code-block :: html+php
141+ of the application are isolated in a new file called ``model.php ``::
147142
148143 // model.php
149144 function open_database_connection()
150145 {
151- $link = mysql_connect('localhost', 'myuser', 'mypassword');
152- mysql_select_db('blog_db', $link);
146+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
153147
154148 return $link;
155149 }
156150
157151 function close_database_connection($link)
158152 {
159- mysql_close( $link) ;
153+ $link = null ;
160154 }
161155
162156 function get_all_posts()
163157 {
164158 $link = open_database_connection();
165159
166- $result = mysql_query('SELECT id, title FROM post', $link);
160+ $result = $link->query('SELECT id, title FROM post');
161+
167162 $posts = array();
168- while ($row = mysql_fetch_assoc( $result)) {
163+ while ($row = $result->fetch(PDO::FETCH_ASSOC )) {
169164 $posts[] = $row;
170165 }
171166 close_database_connection($link);
@@ -182,9 +177,7 @@ of the application are isolated in a new file called ``model.php``:
182177 in this example, only a portion (or none) of the model is actually concerned
183178 with accessing a database.
184179
185- The controller (``index.php ``) is now very simple:
186-
187- .. code-block :: html+php
180+ The controller (``index.php ``) is now very simple::
188181
189182 require_once 'model.php';
190183
@@ -261,21 +254,17 @@ an individual blog result based on a given id::
261254 function get_post_by_id($id)
262255 {
263256 $link = open_database_connection();
264-
265257 $id = intval($id);
266- $query = 'SELECT created_at, title, body FROM post WHERE id = '.$id;
267- $result = mysql_query($query);
268- $row = mysql_fetch_assoc($result);
258+ $result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id);
259+ $row = $result->fetch(PDO::FETCH_ASSOC);
269260
270261 close_database_connection($link);
271262
272263 return $row;
273264 }
274265
275266Next, create a new file called ``show.php `` - the controller for this new
276- page:
277-
278- .. code-block :: html+php
267+ page::
279268
280269 require_once 'model.php';
281270
@@ -353,9 +342,7 @@ You're about to take a **big** step with the application. With one file handling
353342all requests, you can centralize things such as security handling, configuration
354343loading, and routing. In this application, ``index.php `` must now be smart
355344enough to render the blog post list page *or * the blog post show page based
356- on the requested URI:
357-
358- .. code-block :: html+php
345+ on the requested URI::
359346
360347 // index.php
361348
@@ -365,19 +352,17 @@ on the requested URI:
365352
366353 // route the request internally
367354 $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
368- if ('/index.php' == $uri) {
355+ if ('/index.php' === $uri) {
369356 list_action();
370- } elseif ('/index.php/show' == $uri && isset($_GET['id'])) {
357+ } elseif ('/index.php/show' === $uri && isset($_GET['id'])) {
371358 show_action($_GET['id']);
372359 } else {
373360 header('Status: 404 Not Found');
374361 echo '<html><body><h1>Page Not Found</h1></body></html>';
375362 }
376363
377364For organization, both controllers (formerly ``index.php `` and ``show.php ``)
378- are now PHP functions and each has been moved into a separate file, ``controllers.php ``:
379-
380- .. code-block :: php
365+ are now PHP functions and each has been moved into a separate file, ``controllers.php ``::
381366
382367 function list_action()
383368 {
@@ -455,9 +440,7 @@ to interpret each request and return a response. To this end, Symfony provides
455440both a :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` and a
456441:class: `Symfony\\ Component\\ HttpFoundation\\ Response ` class. These classes are
457442object-oriented representations of the raw HTTP request being processed and
458- the HTTP response being returned. Use them to improve the blog:
459-
460- .. code-block :: html+php
443+ the HTTP response being returned. Use them to improve the blog::
461444
462445 // index.php
463446 require_once 'vendor/autoload.php';
@@ -468,9 +451,9 @@ the HTTP response being returned. Use them to improve the blog:
468451 $request = Request::createFromGlobals();
469452
470453 $uri = $request->getPathInfo();
471- if ('/' == $uri) {
454+ if ('/' === $uri) {
472455 $response = list_action();
473- } elseif ('/show' == $uri && $request->query->has('id')) {
456+ } elseif ('/show' === $uri && $request->query->has('id')) {
474457 $response = show_action($request->query->get('id'));
475458 } else {
476459 $html = '<html><body><h1>Page Not Found</h1></body></html>';
@@ -482,9 +465,7 @@ the HTTP response being returned. Use them to improve the blog:
482465
483466The controllers are now responsible for returning a ``Response `` object.
484467To make this easier, you can add a new ``render_template() `` function, which,
485- incidentally, acts quite a bit like the Symfony templating engine:
486-
487- .. code-block :: php
468+ incidentally, acts quite a bit like the Symfony templating engine::
488469
489470 // controllers.php
490471 use Symfony\Component\HttpFoundation\Response;
0 commit comments