Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. PDO/mySQL/PHP - when is it OK to NOT use parameter binding, when should I REALLY use it?

PDO/mySQL/PHP - when is it OK to NOT use parameter binding, when should I REALLY use it?

Scheduled Pinned Locked Moved Database
databasequestionphpcssmysql
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    needAbreakNow
    wrote on last edited by
    #1

    I am not quite familiar with SQL injection and the different methods one could use to infiltrate one's database code, so I have been using PDO with parameter binding with most of my implementation. I do find it a bit clumsy and long sometimes and am tempted to just skip it. So my question is - what circumstances can I NOT use parameter binding without worrying about possible hacks - for less critical information? And the flip side - when should really use it? For more sensitive information only like user emails, id's? or? Would appreciate some feedback or links for further reading.

    J M L J 4 Replies Last reply
    0
    • N needAbreakNow

      I am not quite familiar with SQL injection and the different methods one could use to infiltrate one's database code, so I have been using PDO with parameter binding with most of my implementation. I do find it a bit clumsy and long sometimes and am tempted to just skip it. So my question is - what circumstances can I NOT use parameter binding without worrying about possible hacks - for less critical information? And the flip side - when should really use it? For more sensitive information only like user emails, id's? or? Would appreciate some feedback or links for further reading.

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      You should always use parameter binding. One exception only, when there are no parameters. <edit>After reading Mycrofts answer I realize I should change my second sentence to: One exception only, when the parameter is a constant. :) </edit>

      Wrong is evil and must be defeated. - Jeff Ello

      1 Reply Last reply
      0
      • N needAbreakNow

        I am not quite familiar with SQL injection and the different methods one could use to infiltrate one's database code, so I have been using PDO with parameter binding with most of my implementation. I do find it a bit clumsy and long sometimes and am tempted to just skip it. So my question is - what circumstances can I NOT use parameter binding without worrying about possible hacks - for less critical information? And the flip side - when should really use it? For more sensitive information only like user emails, id's? or? Would appreciate some feedback or links for further reading.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        I'm not as fanatical as Jorgen, there is no circumstance where you should NOT use parameter binding. However there are circumstances (most) where you MUST use parameter binding. If a user or application has anything to do with the values then you MUST use parameters. The only time I would risk not using parameters is when there is no external input of the values. An example of where I will risk string queries would be if you have a master table of countries with an Active attribute field. I might use select * from Country where ActiveFlag = 1 from my BL layer. But seeing as I have a code generator that automatically builds the DAL, model and viewmodel code for me I ALWAYS use parameters and stored procedures

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • N needAbreakNow

          I am not quite familiar with SQL injection and the different methods one could use to infiltrate one's database code, so I have been using PDO with parameter binding with most of my implementation. I do find it a bit clumsy and long sometimes and am tempted to just skip it. So my question is - what circumstances can I NOT use parameter binding without worrying about possible hacks - for less critical information? And the flip side - when should really use it? For more sensitive information only like user emails, id's? or? Would appreciate some feedback or links for further reading.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          To be fair, SQL Injection can only occur where the user has influence over the parameter. If your parameter is a constant, or provided by you, there's little risk since the user cannot change the statement that is going to be executed. ..but, parameterized queries are not done "just" to prevent SQL injection, they provide more benefits. While it may take you a minute longer to write it, it makes the code a lot more readable, improving maintainability. There's also an impact on performance[^] if you re-execute the query. That's not something that every hobbyist wants to learn, so the advice has become that it is NEVER OK to NOT use them. I agree with that advice, since adding the code will not have a downside.

          needAbreakNow wrote:

          I do find it a bit clumsy and long sometimes and am tempted to just skip it.

          This will sound rude but that's just being lazy. If it is worth doing at all, it is worth doing it correctly. Upvoted for asking "why am I doing this"; it proves you are actually thinking about what you are doing, and trying to find alternatives.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          1 Reply Last reply
          0
          • N needAbreakNow

            I am not quite familiar with SQL injection and the different methods one could use to infiltrate one's database code, so I have been using PDO with parameter binding with most of my implementation. I do find it a bit clumsy and long sometimes and am tempted to just skip it. So my question is - what circumstances can I NOT use parameter binding without worrying about possible hacks - for less critical information? And the flip side - when should really use it? For more sensitive information only like user emails, id's? or? Would appreciate some feedback or links for further reading.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            needAbreakNow wrote:

            and am tempted to just skip it

            Versus what? If you create a composition then that can be rather complicated as well. Perhaps you are comparing it to simple cases like when you want to add just one parameter? If so I would say that consistency of usage overrides the ease of one-off cases.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups