8000 Conversion from mysql to PDO by iqbalmalik89 · Pull Request #5811 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Conversion from mysql to PDO #5811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions book/from_flat_php_to_symfony2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ persisted to the database. Writing in flat PHP is quick and dirty:

<?php
// index.php
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);

$result = mysql_query('SELECT id, title FROM post', $link);
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');

$result = $link->query('SELECT id, title FROM post');
$result->setFetchMode(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
Expand All @@ -43,7 +43,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
<body>
<h1>List of Posts</h1>
<ul>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<?php while ($row = $result->fetch()): ?>
<li>
<a href="/show.php?id=<?php echo $row['id'] ?>">
<?php echo $row['title'] ?>
Expand All @@ -53,9 +53,9 @@ persisted to the database. Writing in flat PHP is quick and dirty:
</ul>
</body>
</html>

<?php
mysql_close($link);
$link = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should close the cursor here to be equivalent

?>

That's quick to write, fast to execute, and, as your app grows, impossible
Expand Down Expand Up @@ -87,21 +87,22 @@ the code that prepares the HTML "presentation":

<?php
// index.php
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);

$result = mysql_query('SELECT id, title FROM post', $link);

$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');

$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;
}

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:

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -264,11 +265,9 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line looks wrongly indented. It should contain one additional white space. Same for the below line.

$row = $result->fetch(PDO::FETCH_ASSOC);

close_database_connection($link);

Expand Down
0