Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 793507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:09:49+00:00 2026-05-14T22:09:49+00:00

I have recently decided to switch all my current plain mysql queries performed with

  • 0

I have recently decided to switch all my current plain mysql queries performed with php mysql_query to PDO style queries to improve performance, portability and security. I just have some quick questions for any experts in this database interaction tool

  1. Will it prevent injection if all statements are prepared? (I noticed on php.net it wrote ‘however, if other portions of the query are being built up with unescaped input, SQL injection is still possible‘ I was not exactly sure what this meant). Does this just mean that if all variables are run through a prepare function it is safe, and if some are directly inserted then it is not?

  2. Currently I have a connection at the top of my page and queries performed during the rest of the page. I took a look at PDO in more detail and noticed that there is a try and catch procedure for every query involving a connection and the closing of that connection. Is there a straightforward way to connecting and then reusing that connection without having to put everything in a try or constantly repeat the procedure by connecting, querying and closing?

  3. Can anyone briefly explain in layman’s terms what purpose a set_exception_handler serves?

I appreciate any advice from any more experienced individuals.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:09:50+00:00Added an answer on May 14, 2026 at 10:09 pm
    1. There’s no silver bullet of SQL injection prevention. One can have an unsafe query even though you prepare it.

      $sql = "SELECT * FROM MyTable WHERE id = " . $_GET["id"];
      $stmt = $pdo->prepare($sql);
      

      See? Prepare just takes a string and prepares it as an SQL query. You can still interpolate unsafe content into the string before you prepare it. The prepare only sees a string, it doesn’t know whether you wrote it literally or if parts of the string came from untrustworthy sources.

      You can use a parameter placeholder in the SQL query and then when you call execute() on the prepared statement, you supply the dynamic value. But you can use a parameter placeholder only in place of a literal value in an SQL expression — other types of dynamic content in a query can’t be parameterized. See my presentation SQL Injection Myths and Fallacies for examples and lots of other info about SQL injection.

    2. I don’t put every PDO call in a try block. I write a class to encapsulate data access for some logically cohesive portion of my app. When I call that class, I wrap the call in a try block. If anything goes wrong in one of potentially many database access operations within that class, I catch it and deal with it.

    3. You can use set_exception_handler() in lieu of a catch block. If an exception occurs in your app, but you don’t catch it and it bubbles all the way up the stack until it would have aborted the script, this function is called. Imagine your whole PHP script is in one top-level try block, and you’re declaring code that would go in the corresponding catch block.

      I never use set_exception_handler(). After the function runs, your script halts execution anyway, so there’s no opportunity to re-try the operation that spawned the exception. Also it operates at the top-level scope, so you lose the context of exception. The only thing you can do at that point is pretty-print the exception message and bail out. I prefer to handle exceptions closer to their origin, so I can add some information about the context of the exception, or do some other things before the PHP script halts.


    Re your comments:

    You shouldn’t use mysql_real_escape_string() for table names or column names, because the rules for quoting identifiers are different from the rules for quoting literal string values. Just don’t interpolate input from an external or untrusted source into your SQL query.

    I wrote a code example in my presentation using an associative array so that if user input matches a known value, it uses that as a key in the associative array to look up the legitimate name of a table (or column, in my example). This means you don’t have to use any escaping/quoting function, because you don’t interpolate untrusted content into your SQL query. You only interpolate values that you have pre-defined in your associative array.

    Regarding exceptions, what I mean is this (at a high level):

    $domainObject = new MyDomain();
    
    try {
      $domainObject->create_report($formInput);
    } catch (PDOException $e) {
      // Report error politely so the user knows what happened
      // and what they can do to fix it.
    }
    

    The work inside create_report() is complex and probably involves multiple SQL queries, any of which might go wrong in multiple ways. You don’t necessarily need to catch exceptions for every SQL operation inside that function, you could just catch any and all exceptions that pop out of the function, and deal with them in one place, in the code that calls create_report().

    Also, you probably don’t want to just spew the verbatim exception message at them, since they won’t know what to make of that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently decided that I just have to finally learn C/C++, and there is
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have recently started having problems with TortoiseCVS, or more specifically with plink, the
I have recently installed .net 3.5 SP1. When I deployed a compiled web site
I have recently upgraded some of my web applications to ASP.NET 3.5 by installing
I have recently written an application(vb.net) that stores and allows searching for old council
I have recently started using Vim as my text editor and am currently working
I have recently been doing a bit of investigation into the different types of
I have recently run across these terms few times but I am quite confused
We have recently migrated a large, high demand web application to Tomcat 5.5 from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.