Just What Is PDO!

Hi,

I have almost finished my site in MySQL. However I am often told that it should be in PDO. The problem is I just dont understand what PDO is.

How different is to PHP, what has PDO been introduced? Why is PHP being deprecated just for it to be replaced by another code?

I have read through a number of sites but I just dont understand it.

Take this as an example…how do I convert this into PDO?

	<?php
$query = "SELECT * FROM users WHERE id = " . intval($_SESSION['userID']) . " LIMIT 1";
if ($result = mysql_query($query)) {

                $row = mysql_fetch_array($result);
		{
                ?>		

There are three different interfaces currently available to communicate between PHP and MySQL.

mysql_ which is currently obsolete and will be deleted from the next version of PHP
mysqli_ which is the improved direct replacement
PDO which offers a database independent interface (and which supports databases other than mySQL allowing switching databases to be done much easier)

You should currently be using either the second or third of these - there’s no reason to switch from mysqli_ to PDO unless you expect to switch away from MySQL. Any mysql_ use that you currently have should be converted to one of the other two as soon as possible.

Thanks but how do I convert it?

I have absolutely no idea what PDO looks like.

( I have read on other forums that MySQL will not be deprecated as it will destroy many thousands of websites.)

Here’s a good introduction to PDO and how it compares to mysql_ code.

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

Hi,

I tried to connect to the database using this, it creates the following error: “(using password: NO)”

I thought this PDO was meant to be easier!

<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

I have also seen it like this. Do I need to say $username = myusername before this?

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

Hi,

I wanted to build a site which I have almost completed however I built the site in MySQL as per below. The problem I have now is that people are telling me that it should be in PDO. The problem is I am finding it very hard to get to grips with PDO. Much harder than MySQL.

For example if this is going to become deprecated how can I update it into PDO?

				<?php
$query = mysql_query("SELECT eventname, firstname, eventdetails, eventlocation, eventcountry, eventid, eventstart, eventend, logo, company, id, supplierdetails1, eventid
FROM users
JOIN eventjobs ON eventjobs.organiserid = users.id
ORDER BY eventid DESC
 LIMIT 8	");	
while($row = mysql_fetch_array($query)) {
?>

I’d disagree that there’s no reason to switch from mysqli_ to PDO unless you which to switch away from mysql for one reason - it has better prepared statements than the mysqli_ functionality, because it can accept named variables in prepared statements.

For the original poster: prepared statements are much more secure than writing a standard sql query and relying on mysqli_real_escape_string and other such functions, and while both the mysqli_* library and the PDO library support this functionality, it’s better in PDO and I’d therefore recommend PDO for this reason.

Thanks,

But I am struggling to use PDO. Are there any very basic templates that I can use to practice with?