@@ -29,12 +29,12 @@ 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);
34-
35- $result = mysql_query('SELECT id, title FROM post', $link );
32+ $link = new PDO("mysql:host= localhost;dbname=blog_db" , 'myuser', 'mypassword');
33+
34+ $result = $link->query('SELECT id, title FROM post');
35+ $result->setFetchMode(PDO::FETCH_ASSOC );
3636 ?>
37-
37+
3838 <!DOCTYPE html>
3939 <html>
4040 <head>
@@ -43,7 +43,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
4343 <body>
4444 <h1>List of Posts</h1>
4545 <ul>
46- <?php while ($row = mysql_fetch_assoc( $result)): ?>
46+ <?php while ($row = $result->fetch( )): ?>
4747 <li>
4848 <a href="/show.php?id=<?php echo $row['id'] ?>">
4949 <?php echo $row['title'] ?>
@@ -53,9 +53,9 @@ persisted to the database. Writing in flat PHP is quick and dirty:
5353 </ul>
5454 </body>
5555 </html>
56-
56+
5757 <?php
58- mysql_close( $link) ;
58+ $link = null ;
5959 ?>
6060
6161That's quick to write, fast to execute, and, as your app grows, impossible
@@ -86,21 +86,22 @@ the code that prepares the HTML "presentation":
8686.. code-block :: html+php
8787
8888 // index.php
89- $link = mysql_connect(' localhost' , 'myuser', 'mypassword');
90- mysql_select_db('blog_db', $link);
91-
92- $result = mysql_query('SELECT id, title FROM post', $link );
93-
89+ $link = new PDO("mysql:host= localhost;dbname=blog_db" , 'myuser', 'mypassword');
90+
91+ $result = $link->query('SELECT id, title FROM post');
92+ $result->setFetchMode(PDO::FETCH_ASSOC );
93+
9494 $posts = array();
95- while ($row = mysql_fetch_assoc( $result)) {
95+ while ($row = $result->fetch( )) {
9696 $posts[] = $row;
9797 }
98-
99- mysql_close( $link) ;
100-
98+
99+ $link = null ;
100+
101101 // include the HTML presentation code
102102 require 'templates/list.php';
103103
104+
104105The HTML code is now stored in a separate file (``templates/list.php ``), which
105106is primarily an HTML file that uses a template-like PHP syntax:
106107
@@ -148,28 +149,28 @@ of the application are isolated in a new file called ``model.php``:
148149 // model.php
149150 function open_database_connection()
150151 {
151- $link = mysql_connect('localhost', 'myuser', 'mypassword');
152- mysql_select_db('blog_db', $link);
153-
152+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
154153 return $link;
155154 }
156-
155+
157156 function close_database_connection($link)
158157 {
159- mysql_close( $link) ;
158+ $link = null ;
160159 }
161-
160+
162161 function get_all_posts()
163162 {
164163 $link = open_database_connection();
165-
166- $result = mysql_query('SELECT id, title FROM post', $link);
164+
165+ $result = $link->query('SELECT id, title FROM post');
166+ $result->setFetchMode(PDO::FETCH_ASSOC);
167+
167168 $posts = array();
168- while ($row = mysql_fetch_assoc( $result)) {
169+ while ($row = $result->fetch( )) {
169170 $posts[] = $row;
170171 }
171172 close_database_connection($link);
172-
173+
173174 return $posts;
174175 }
175176
@@ -261,11 +262,9 @@ an individual blog result based on a given id::
261262 function get_post_by_id($id)
262263 {
263264 $link = open_database_connection();
264-
265265 $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);
266+ $result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id);
267+ $row = $result->fetch(PDO::FETCH_ASSOC);
269268
270269 close_database_connection($link);
271270
0 commit comments