-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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'] ?> | ||
|
@@ -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; | ||
?> | ||
|
||
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": | |
|
||
<?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: | ||
|
||
|
@@ -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,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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