@@ -29,12 +29,12 @@ persisted to the database. Writing in flat PHP is quick and dirty:
29
29
30
30
<?php
31
31
// 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 );
36
36
?>
37
-
37
+
38
38
<!DOCTYPE html>
39
39
<html>
40
40
<head>
@@ -43,7 +43,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
43
43
<body>
44
44
<h1>List of Posts</h1>
45
45
<ul>
46
- <?php while ($row = mysql_fetch_assoc( $result)): ?>
46
+ <?php while ($row = $result->fetch( )): ?>
47
47
<li>
48
48
<a href="/show.php?id=<?php echo $row['id'] ?>">
49
49
<?php echo $row['title'] ?>
@@ -53,9 +53,9 @@ persisted to the database. Writing in flat PHP is quick and dirty:
53
53
</ul>
54
54
</body>
55
55
</html>
56
-
56
+
57
57
<?php
58
- mysql_close( $link) ;
58
+ $link = null ;
59
59
?>
60
60
61
61
That's quick to write, fast to execute, and, as your app grows, impossible
@@ -86,21 +86,22 @@ the code that prepares the HTML "presentation":
86
86
.. code-block :: html+php
87
87
88
88
// 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
+
94
94
$posts = array();
95
- while ($row = mysql_fetch_assoc( $result)) {
95
+ while ($row = $result->fetch( )) {
96
96
$posts[] = $row;
97
97
}
98
-
99
- mysql_close( $link) ;
100
-
98
+
99
+ $link = null ;
100
+
101
101
// include the HTML presentation code
102
102
require 'templates/list.php';
103
103
104
+
104
105
The HTML code is now stored in a separate file (``templates/list.php ``), which
105
106
is primarily an HTML file that uses a template-like PHP syntax:
106
107
@@ -148,28 +149,28 @@ of the application are isolated in a new file called ``model.php``:
148
149
// model.php
149
150
function open_database_connection()
150
151
{
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');
154
153
return $link;
155
154
}
156
-
155
+
157
156
function close_database_connection($link)
158
157
{
159
- mysql_close( $link) ;
158
+ $link = null ;
160
159
}
161
-
160
+
162
161
function get_all_posts()
163
162
{
164
163
$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
+
167
168
$posts = array();
168
- while ($row = mysql_fetch_assoc( $result)) {
169
+ while ($row = $result->fetch( )) {
169
170
$posts[] = $row;
170
171
}
171
172
close_database_connection($link);
172
-
173
+
173
174
return $posts;
174
175
}
175
176
@@ -261,11 +262,9 @@ an individual blog result based on a given id::
261
262
function get_post_by_id($id)
262
263
{
263
264
$link = open_database_connection();
264
-
265
265
$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);
269
268
270
269
close_database_connection($link);
271
270
0 commit comments