-
-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Conversion from mysql to PDO because mysql is deprecated.
- Loading branch information
There are no files selected for viewing
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 s 8000 yntax: | ||
|
||
|
@@ -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'); | ||
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. indentation should be fixed here as well |
||
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(); | ||
|
||
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. please do not introduce trailing whitespace |
||
$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); | ||
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 seems like a wrong indentation 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. Yes I will fix all these corrections today. Thanks for checking. |
||
|
||
close_database_connection($link); | ||
|
||
return $row; | ||
} | ||
|
||
|
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