From 5f796f4be94e990420f2b617583593f15a8bee43 Mon Sep 17 00:00:00 2001 From: Muhammad Iqbal Date: Tue, 20 Oct 2015 00:59:35 +0500 Subject: [PATCH 1/3] Conversion from mysql to PDO Conversion from mysql to PDO because mysql is deprecated. --- book/from_flat_php_to_symfony2.rst | 66 +++++++++++++++--------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index 2ee49fc7851..d9b28f5ab55 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -29,12 +29,12 @@ persisted to the database. Writing in flat PHP is quick and dirty: query('SELECT id, title FROM post'); + $result->setFetchMode(PDO::FETCH_ASSOC); ?> - + @@ -43,7 +43,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:

List of Posts

- + That's quick to write, fast to execute, and, as your app grows, impossible @@ -87,21 +87,22 @@ the code that prepares the HTML "presentation": query('SELECT id, title FROM post'); + $result->setFetchMode(PDO::FETCH_ASSOC); + $posts = array(); - while ($row = mysql_fetch_assoc($result)) { + while ($row = $result->fetch()) { $posts[] = $row; } - - mysql_close($link); - + + $link = null; + // include the HTML presentation code require 'templates/list.php'; + The HTML code is now stored in a separate file (``templates/list.php``), which is primarily an HTML file that uses a template-like PHP syntax: @@ -150,28 +151,28 @@ of the application are isolated in a new file called ``model.php``: // model.php function open_database_connection() { - $link = mysql_connect('localhost', 'myuser', 'mypassword'); - mysql_select_db('blog_db', $link); - + $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword'); return $link; } - + function close_database_connection($link) { - mysql_close($link); + $link = null; } - + function get_all_posts() { $link = open_database_connection(); - - $result = mysql_query('SELECT id, title FROM post', $link); + + $result = $link->query('SELECT id, title FROM post'); + $result->setFetchMode(PDO::FETCH_ASSOC); + $posts = array(); - while ($row = mysql_fetch_assoc($result)) { + while ($row = $result->fetch()) { $posts[] = $row; } close_database_connection($link); - + return $posts; } @@ -264,14 +265,13 @@ an individual blog result based on a given id:: function get_post_by_id($id) { $link = open_database_connection(); - + $id = intval($id); - $query = 'SELECT created_at, title, body FROM post WHERE id = '.$id; - $result = mysql_query($query); - $row = mysql_fetch_assoc($result); - + $result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id); + $row = $result->fetch(PDO::FETCH_ASSOC); + close_database_connection($link); - + return $row; } From 7053ae72b2d8789040f3f3325dac254c76549008 Mon Sep 17 00:00:00 2001 From: Muhammad Iqbal Date: Sat, 19 Dec 2015 01:34:48 +0500 Subject: [PATCH 2/3] Update from_flat_php_to_symfony2.rst --- book/from_flat_php_to_symfony2.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index d9b28f5ab55..0dcc7973c5d 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -151,7 +151,7 @@ of the application are isolated in a new file called ``model.php``: // model.php function open_database_connection() { - $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword'); + $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword'); return $link; } @@ -265,13 +265,12 @@ an individual blog result based on a given id:: function get_post_by_id($id) { $link = open_database_connection(); - $id = intval($id); - $result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id); - $row = $result->fetch(PDO::FETCH_ASSOC); - + $result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id); + $row = $result->fetch(PDO::FETCH_ASSOC); + close_database_connection($link); - + return $row; } From 4badd17cc7742f297e6a49e383d5ecfac363e0f4 Mon Sep 17 00:00:00 2001 From: Muhammad Iqbal Date: Sat, 19 Dec 2015 01:36:04 +0500 Subject: [PATCH 3/3] Update from_flat_php_to_symfony2.rst Indentation Fixed --- book/from_flat_php_to_symfony2.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index 0dcc7973c5d..c4aa3a7e6a5 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -164,8 +164,8 @@ of the application are isolated in a new file called ``model.php``: { $link = open_database_connection(); - $result = $link->query('SELECT id, title FROM post'); - $result->setFetchMode(PDO::FETCH_ASSOC); + $result = $link->query('SELECT id, title FROM post'); + $result->setFetchMode(PDO::FETCH_ASSOC); $posts = array(); while ($row = $result->fetch()) {