SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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?


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

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    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.
    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.

  4. #4
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

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

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.)

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Here's a good introduction to PDO and how it compares to mysql_ code.

    http://wiki.hashphp.org/PDO_Tutorial...SQL_Developers
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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!

    Code:
    <?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?

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

  8. #8
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Query PDO

    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?


    Code:
    				<?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)) {
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •