+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11
Like Tree11Likes

Thread: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

  1. #1
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Exclamation PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    While there won't be any immediate effect, the PHP developers have finally decided to begin the long, slow, soft process of deprecating the mysql extension. Anyone doing development in PHP using MySQL as a database back-end should have stopped using the mysql methods long ago (it has been superceded by two newer methods over the years, first by mysqli, then by PHP Data Objects (PDO)), but it has taken until now for the first warning shot to be fired.

    For those not aware of the dangers, using the PHP mysql extension is one of the quickest and easiest ways of making your site vulnerable to SQL injection attacks. In addition, it forces you to handle all of the details of sanitising (preparing) data for storage in your database, and the follies of string escaping have provided a consistent source of laughs over the years (have you ever seen the ever-growing forest of backslashes as you page through search results?).

    There have been better ways of doing things for quite a while now. Unfortunately, there are also a lot of code snippets and tutorials on the web that still use the mysql extension, and many of those are on sites that claim to be authoritative (I'm looking at you, W3Schools). And now, knowing that ext/mysql is going to go away, there is no longer any excuse for using it in new development. Stay far, far away from any tutorial or library that encourages you to use the mysql extension -- if you are searching for PHP snippets or tutorials, make sure you include PDO in your search query.

    Just one small suggestion when using PDO, though -- do not use the ? (ordered parameters) syntax when preparing statements; use the :variable (named parameters) syntax instead. It'll keep you out of the asylum.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    I'd give slightly different advice about positional parameters. A positional parameter is fine if a statement has only one parameter and will only ever have one parameter. Multiple positional parameters are also fine if the statement is programmatically generated and executed, such as for an ORM. More than one parameter in a hand-written statement and named is definitely the way to go.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    I <3 this thread
    alfred4w likes this.
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  4. #4
    Skizzerz's Avatar
    Skizzerz is offline Contributors Skizzerz will become famous soon enough
    Join Date
    Nov 2007
    Location
    Texas
    Posts
    2,153

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    So do I -- stickied.
    callumacrae likes this.
    Ryan Schmidt | Level 2 Support
    █ 888-X10-9668 - ryan[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  5. #5
    alfred4w is offline x10Hosting Member alfred4w is an unknown quantity at this point
    Join Date
    Dec 2011
    Posts
    3

    Cool Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    nice...

  6. #6
    miguelkp's Avatar
    miguelkp is offline x10 Lieutenant miguelkp is an unknown quantity at this point
    Join Date
    Oct 2009
    Location
    El Bierzo - Spain
    Posts
    302

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    Here is the best article/tutorial about PHP PDO usage (including examples) I've found, for now al least:

    http://www.phpro.org/tutorials/Intro...o-PHP-PDO.html
    Last edited by miguelkp; 03-28-2012 at 07:59 AM.
    Clic on userbar to visit my band's website:

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    That one has a few issues (use of SELECT *, extraneous information, information disclosure in error handling code). As is often the case with tutorials online, I've yet to see one that should be used to the exclusion of others. It's usually best to read multiple tutorials, so you don't get stuck in one way of thinking.
    In the end, it's most important to read the PDO manual pages for any information left out of the tutorials.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    miguelkp's Avatar
    miguelkp is offline x10 Lieutenant miguelkp is an unknown quantity at this point
    Join Date
    Oct 2009
    Location
    El Bierzo - Spain
    Posts
    302

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    True: best comparing various tutorials, instead of using only one. And thanks for the links.
    I also use the last one you put (the official php.net documentation). The best, obviously, but first time I read it, I was a bit lost.

    So what I like in the one I put is that it follows similar structure than the old mysql extension tutorials. I mean, it explains kinda 'step-by-step' how to connect, how to work with opened database (queries) and how to close connection; ie, it tells you what functions (or rather, methods) you have to look for in php.net to get the finest information. That way is the one I'm using right now and I'm finding easier to move from old mysql extension to PDO sentences.
    Clic on userbar to visit my band's website:

  9. #9
    xdevx10m is offline x10Hosting Member xdevx10m is an unknown quantity at this point
    Join Date
    May 2012
    Posts
    1

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    I switched to mysqli 2 years ago. PDO is nice but mysqli is much easier and it supports prepared statements as well.
    http://php.net/manual/en/mysqli.prepare.php

    For those who stucked with old mysql ext this tool can help a lot. (Don't forget to backup your old app just in case)
    http://forge.mysql.com/wiki/Converting_to_MySQLi

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP to begin deprecation of ext/mysql -- start moving your development to PDO now

    Quote Originally Posted by xdevx10m View Post
    PDO is nice but mysqli is much easier and it supports prepared statements as well.[/URL]
    mysqli is simpler as a drop-in replacement for the outdated mysql extension, as the interfaces are similar, but PDO is generally considered the easier to use and more expressive extension, requiring fewer method calls to achieve the same affect. The API Overview on MySQL.com says: "PDO has its advantages, such as a clean, simple, portable API". The reason to use mysqli over PDO is that the former exposes advanced MySQL features. PDO is a generic DB interface, so its MySQL driver can't expose many MySQL specific features without causing compatibility issues (differences between RDBMSs in SQL support cause enough problems).

    Things are slightly better for mysqli in PHP 5.4, when support for the Traversable interface was added to mysqli_result. If you must have 5.3 compatibility, however, PDO's support for Traversable is a big win when it comes to abstraction. You can use a PDOStatement in the place of an array or other collection type if all that's needed is to iterate over it (with foreach). You can use PDOStatement::setFetchMode to fetch results as objects for some real magic.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-30-2011, 11:54 AM
  2. Replies: 8
    Last Post: 12-14-2009, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers