/    Sign up×
Community /Pin to ProfileBookmark

php pdo fetch and query

im doing a short tutorial on php pdo and the content isnt that detailed so i have some questions.

  • 1. with the query and fetch functions. couldnt a user do sql injection? when youre inserting data theres the prepared statements which remove swl injection, what safeguards are there against that when querying?

  • 2. what is the advantage of using fetch in a while loop over just using a foreach loop with the query function?

  • to post a comment
    PHP

    7 Comments(s)

    Copy linkTweet thisAlerts:
    @NogDogApr 12.2019 — 
  • 1. My preferred solution is to always use a prepared statement with bound parameters when possible if external data is being used. If that's not feasible for some reason, then you can use the [u][PDO::quote()](https://www.php.net/manual/en/pdo.quote.php)[/u] function to escape any string input.


  • 2. I don't think there's any "advantage" other than personal style preferences, or more likely the fact that many users probably don't recognize/understand that it's possible since PDOStatement implements Traversable? I've never actually tried it with a prepared statement, which is what I use 99% of the time, so I might have to try that some time and see. :)


  • Anyway, if I were writing the tutorial, I'd push users toward prepared statements first, and use PDO::query() only in simple cases where there is zero benefit to using a prepared statement.
    Copy linkTweet thisAlerts:
    @coleioauthorApr 13.2019 — @NogDog#1602749 so I can use pdo::quote on the user input then pass it to the SQL statement? Does this need to be done when using prepared statements and binding? Is it possible to use prepared statements and binding when querying?
    Copy linkTweet thisAlerts:
    @NogDogApr 13.2019 — Using bound parameters when executing a prepared statement automatically takes care of any escaping and quoting. PDO's option of using named place-holders makes it easy to see which parameter is used where in the query, as opposed to the only option in MySQLi of using "?" place-holders. Also, you can do the binding implicitly via an array parameter of the execute() method, helping to keep things clean.
    <i>
    </i>$sql = "
    SELECT * FROM my_table
    WHERE foo = :foo and bar = :bar
    ORDER BY foo
    ";
    $stmt = $pdo-&gt;prepare($sql);
    $stmt-&gt;execute(array(
    ':foo' =&gt; $_GET['foo'],
    ':bar' =&gt; $_GET['bar']
    ));
    Copy linkTweet thisAlerts:
    @coleioauthorApr 13.2019 — Why use the query at all then if that's possible?
    Copy linkTweet thisAlerts:
    @coleioauthorApr 13.2019 — am i correct in saying that query should only be used when you know data isnt dodgy?
    Copy linkTweet thisAlerts:
    @NogDogApr 14.2019 — > @coleio#1602768 am i correct in saying that query should only be used when you know data isnt dodgy?

    Basically, yes. If you are using any variable values that have the slightest possibility of being insecure, then a prepared statement with bound parameters is the way to go, in my opinion. You can use PDO::query() if you also apply PDO::quote() to variable values and such, or cast to a proper numeric type if applicable -- but I find it's easier to just let preparing with bound parameters cleaner and less prone to error.
    Copy linkTweet thisAlerts:
    @ginerjmApr 14.2019 — A simple query call can also be used when you are NOT relying on user input for the arguments in the query statement. Such as if you are just looking for a record with a value of "abc" in it and your script has determined that value on its own, then a quick query call can be used. It is when you are using input that may have problems that you want to use the prepared approach.
    ×

    Success!

    Help @coleio spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.26,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...