Skip to content
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

Patch 1 #3

Open
wants to merge 4 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<meta property="og:url" content="http://www.phptherightway.com"/>
<meta property="og:site_name" content="PHP: The Right Way"/>
<meta property="og:type" content="website"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Alfa+Slab+One|Droid+Serif"/>
<link rel="stylesheet" href="/styles/all.css"/>
<link rel="stylesheet" href="/styles/syntax.css"/>
Expand Down Expand Up @@ -41,6 +42,11 @@
</ul>
</nav>
<div class="site-content">

<a href="https://github.com/codeguy/php-the-right-way">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub">
</a>

<header class="site-header" id="site-header">
<hgroup>
<h1 class="site-title"><a href="/">PHP</a></h1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
---
title: Databases and PDO
title: Databases
---

# Databases and PDO
# Databases

Many times your PHP code will use a database to persist information. If you use a database, use `PDO` to talk with it. PDO is a
database connection abstraction library &mdash; built into PHP since 5.1.0 &mdash; that provides a common interface to talk with
many different databases. PDO will not translate your SQL queries or emulate missing features, it is purely for connecting to multiple
types of database with the same API.
Many times your PHP code will use a database to persist information. If you use a database you have a few options to connect and interact
with your database. The recommended option until PHP 5.1.0 was always to use native drivers such as [mysql][mysql], [mysqli][mysqli], [pgsql][pgsql], etc.

Native drivers are great if you are only using ONE database in your application, but if for example you are using MySQL and a little bit of MSSQL, or need to connect to an Oracle database, then you will not be able to use the same drivers. You'll need to learn a brand new API for
each database and that can get silly.

As an extra note on native drivers, the mysql extension for PHP is currently deprecated as of PHP 5.4.0 and will be removed entirely in PHP 5.5.0. That means if you are using `mysql_connect()` and `mysql_query()` in your applications then you will be faced with a rewrite when
you upgrade to the next version. You can rewrite this application now to use the [MySQLi extension][mysqli], or use PDO.

## PDO

PDO is a database connection abstraction library &mdash; built into PHP since 5.1.0 &mdash; that provides a common interface to talk with
many different databases. PDO will not translate your SQL queries or emulate missing features, it is purely for connecting to multiple types
of database with the same API.

More importantly, `PDO` allows you to safely inject foreign input (e.g. IDs) into your SQL queries without worrying about database SQL injection attacks. This is possible using PDOStatements and bound parameters.

Expand Down Expand Up @@ -52,3 +62,7 @@ Some abstraction layers have been built using the PSR-0 namespace standard so ca
[2]: http://www.doctrine-project.org/projects/dbal.html
[3]: http://framework.zend.com/manual/en/zend.db.html
[4]: http://packages.zendframework.com/docs/latest/manual/en/zend.db.html

[mysql]: http://uk.php.net/mysql
[mysqli]: http://uk.php.net/mysqli
[pgsql]: http://uk.php.net/pgsql
2 changes: 1 addition & 1 deletion _posts/07-04-01-Data-Filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PHP provides the `filter_var` and `filter_input` functions to help you do this.

### Sanitization

Sanitization removes (or escapes) illegal or unsafe characters from foreign input. For example, you should sanitize foreign input before including the input in HTML or inserting it into a raw SQL query. When you use bound parameters with [PDO](#databases_and_pdo), it will sanitize the input for you.
Sanitization removes (or escapes) illegal or unsafe characters from foreign input. For example, you should sanitize foreign input before including the input in HTML or inserting it into a raw SQL query. When you use bound parameters with [PDO](#databases), it will sanitize the input for you.

[See Sanitization Filters][2]

Expand Down