Thread: MySQLi OOP

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2

    MySQLi OOP


    So I'm a couple weeks and several thousand lines of code into my current project. I'm still learning PHP and it has recently come to my attention that MySQL functions are being deprecated. Unfortunately I need to go back and change everything MYSQL related to MySQLi.

    I feel a little stressed with a long road ahead of me and not knowing much beyond the basics of OOP.

    Is it possible to write the four basic CRUD functions inside a class and simply pass the SQL to the given function needed as a variable, or will I be required to write out queries and while loops for every single query?

    The latter seems like a daunting task and very messy compared to just using the old MySQL syntax that is being phased out.

    Any help or suggestions would be greatly appreciated.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    first of all: If your current database code is OK and secure (with every value wrapped in mysql_real_escape_string), you do not need to switch to MySQLi now. The old extension is indeed deprecated, but that doesn't mean it will be removed tomorrow. The PHP developers are well aware that this would break many websites, so they will certainly keep the extensions for some more years.

    Personally, I'm a strong advocate of MySQLi and PDO, but only if you actually use the new features (like prepared statements). If you merely replace mysql_query() with mysqli_query(), you haven't really gained anything.

    So don't panic, don't rush things. If you think you need an update, do it gradually on a test system and replace the old query functions "by hand" so that you can use the new features.

    A database wrapper would certainly make sense in order to be independend of the underlying extension. But you don't need to write your own, because PHP has one already: PDO. Unlike MySQLi, it isn't bound to a particular database system. You can use it with all mainstream DBMS like MySQL, PostgreSQL, Oracle etc.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Thanks that really put my mind at ease. I'll finish up the project and work on having a solid understanding of PDO prior to implementing the switch.

    Comments on this post

    • Jacques1 agrees
  6. #4
  7. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    Everything that Jacques1 said is very true. But I thought that I should mention that mysqli supports object oriented calls AND procedural calls. Thus, if you have written a lot of procedural code, you do not have to replace it all with OO code to use mysqli. You can take advantage of all the new functionality and still use your procedural code. I found myself in exactly your position a few months ago and am slowly converting from mysql to mysqli while sticking to procedural code. i found this web site very useful: http://www.phpknowhow.com/mysql/mysqli-procedural-functions/
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,985
    Rep Power
    375
    if your editor allows you to do replace in files/project then you could simply replace all

    mysql calls with mysqli equivalent calls
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,053
    Rep Power
    9398
    Originally Posted by paulh1983
    if your editor allows you to do replace in files/project then you could simply replace all

    mysql calls with mysqli equivalent calls
    There are a few big differences. Like procedural mysqli requires the connection to be passed, and the order of arguments generally has the link first (like mysql_query() has query+link while mysqli_query() has link+query).
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    ... and like I already said above: You gain absolutely nothing from simply replacing the function calls. I mean, it's not like mysqli_query() was somehow "better" than mysql_query().

    If you do an update, then do it right and actually use the new features. Otherwise it's just "Using the latest stuff makes me feel better".
  14. #8
  15. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Unfortunately I need to go back and change everything MYSQL related to MySQLi
    This is where (and why) developers write in database abstraction layers.

    If you had written your own code/function to wrap around your mysql_* implementation then you'd only need to change your code in one place.

    Design patterns like MVC also come into play here - having all your database code/functions/connectivity all laid out in one folder of your project then at least you know where all your code is.

    If you have "spaghetti" code with php and html all over the place with database connections and queries thrown in as and when they're needed (almost ad hoc style) then your code base is going to be a nightmare to maintain at any stage, regardless of database functions (this is just your first taste of it).

    I don't necessarily recommend MVC, as i don't think it's always fit for purpose on websites, but the principle is a good fit to a lot of situations requiring logic, data and display.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo