I Object – Part 2

i-objectI was thinking of writing a little tutorial on PHP objects but there is an excellent one available at Nettuts.  As a brief example though, PHP is frequently used to connect to a MySQL database.  In the past, I would have used code such as this example also from Nettuts:

mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());

There is now an object orient equivalent which uses the PHP Data Object or PDO:

$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);

Using the PDO method, you need to prepare your database query with placeholders if need and then execute them.  Although this adds an additional step, it makes SQL injection(attacks where strings are added to the query ) attacks virtually impossible.



About Robert

Robert is a novice web site developer. A lack of professional training has not prevented him from creating some very nice websites for small businesses who would otherwise have gone without a web presence.


Subscribe to our e-mail newsletter to receive updates.

No comments yet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.