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

Thread: PHP mysqli_real_escape_string PDO equivilent

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    PHP mysqli_real_escape_string PDO equivilent

    I was wondering whether PDO had a function equivilent of the mysqli_real_escape_string to prevent SQL injections, or is there some specific code to prevent SQL injections which would mirror the effect of the mysqli_real_escape_string()?
    Last edited by as4s1n; 04-05-2010 at 12:46 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: PHP mysqli_real_escape_string PDO equivilent

    Quote Originally Posted by as4s1n View Post
    I was wondering whether PDO had a function equivilent of the mysqli_real_escape_string to prevent SQL injections, or is there some specific code to prevent SQL injections which would mirror the effect of the mysqli_real_escape_string()?
    It’s not recommended, but if you absolutely need to build your SQL statement including values all at once, you can use PDO::quote() http://us2.php.net/manual/en/pdo.quote.php

    Actually, it's much better to use PDO::prepare() http://us2.php.net/manual/en/pdo.prepare.php. This will separate the SQL statement and the values, ergo preventing the injection, and that’s why you don’t need to use mysql_real_escape_string().

    What happens with PDO::prepare is that before sending values, it sends to the database engine all the information it needs with placeholders for the values, so later when you send the actual values you can’t trick the engine to think that the value ended adding some malicious stuff.

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP mysqli_real_escape_string PDO equivilent

    Oh OK, it really didn't explain it well on the site so I wanted to see.

    BTW: Does the query() statement have the same protection as the prepare?
    Last edited by as4s1n; 04-05-2010 at 07:10 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

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

    Re: PHP mysqli_real_escape_string PDO equivilent

    Quote Originally Posted by as4s1n View Post
    Oh OK, it really didn't explain it well on the site so I wanted to see.
    What site is that? Make sure you read the PDO prepared statement documentation on the PHP site; it explains this right off the bat.

    Quote Originally Posted by as4s1n View Post
    BTW: Does the query() statement have the same protection as the prepare?
    Only prepared statement parameters are invulnerable to SQL injection because they are sent out-of-band. Since only simple values can be parameterized, other parts of prepared statements (identifiers, keywords, clauses) are vulnerable. PDO::query doesn't use parameters, so it's entirely vulnerable. PDO::query should only be used for static queries executed once.
    Last edited by misson; 04-05-2010 at 09:03 PM.
    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.

  5. #5
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP mysqli_real_escape_string PDO equivilent

    Does that mean it would be unneccessary to include the fields in the query

    E.G. $sth = $dbh->prepare("INSERT INTO table_name(field1,field2,field3, ...etc) VALUES ...");
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

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

    Re: PHP mysqli_real_escape_string PDO equivilent

    You should always specify the fields in an INSERT statement, so that it will continue to work should you change the table schema by adding or reordering fields. It has nothing to do with protecting against SQL injection.
    Last edited by misson; 04-08-2010 at 03:08 PM.
    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.

  7. #7
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP mysqli_real_escape_string PDO equivilent

    Oh, well the book I read on it says that that protects from SQL injection because it keeps it in order and if its not then it will input the whole one statement and ignore the rest... But now I'm not sure.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

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

    Re: PHP mysqli_real_escape_string PDO equivilent

    Explicitly specifying the columns would make it slightly harder to produce a query that executes without error, but only slightly. SQL injection would still be entirely possible. Since prepared statement parameters are completely immune to SQL injection, explicit columns won't matter as far as injection is concerned. Explicit column specifications in queries are there to add readability and robustness, making the query independent of table structure (which is one of the main reasons we have relational databases, codified as Codd's 9th rule of logical data independence).

    Prepared statements send the statement separate from the parameter values. As a result, anything in the values is safe. In JSON notation, a query is something like:
    Code:
        Query = {
            statement: "INSERT INTO tbl (col1, col2, col3) VALUES (:field1, :field2, :field3)",
            values: {':field1': 'foo', ':field2': 'bar', ':field3': 42}
        }
    except that the statement is parsed only once rather than sending it as a string with each query. There's simply no way that any data in the values can be confused for any part of the statement, thus no injection attack. Any data that's placed in the statement is part of the statement; the statement itself is still vulnerable, but there's rarely a need to interpolate user data into the statement, so this isn't much of an issue.
    Last edited by misson; 04-08-2010 at 09:40 PM.
    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.

  9. #9
    tillabong is offline x10Hosting Member tillabong is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    60

    Re: PHP mysqli_real_escape_string PDO equivilent

    pardon me for the intrusion. im just wondering that is it only with GET methods from url or values you retrieve from submitted forms to make a query that Sql injection occurs?

    if a page only connects to the database to retrieve information but doesnt allow the user to key in any values of any sort, would the connection to mysql itself be subjected to sql injection?

    sorry im kinda new to sql. hope to learn a thing or two from you guys.

  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 mysqli_real_escape_string PDO equivilent

    Don't threadjack. You'll attract more new posters with a new thread than with an established thread.
    Last edited by misson; 04-09-2010 at 06:36 PM.
    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

LinkBacks (?)


Similar Threads

  1. Is there an open source cPanel equivilent?
    By n1web in forum Computers & Technology
    Replies: 2
    Last Post: 05-21-2009, 06:43 AM

Tags for this Thread

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