Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As long as you sanitize data appropriately for your queries then you don't <strong>have</strong> to use PDO/prepared statements. <em>Though</em>, I would personally recommend using PDO/prepared statements simply because they do both make development/debugging easier and prepared statements prevent incorrect data types from even getting in to a query.</p> <p>If you want to learn more about how to create a simple prepared statement look in to the function <code>sprintf</code>. You simply replace any variable strings, integers, etc with a type specifier (in this case <code>%s</code> and <code>%d</code> respectively).</p> <p>So for example in the following query, I know that <code>id</code> is going to be an integer (it will be numerical) and <code>name</code> will be a string (alphanumeric).</p> <pre><code>$username = 'Simon'; $id = 3; $query = "SELECT FROM `users` WHERE `id` = {$id} AND `name` = '{$username}'"; </code></pre> <p>If I'm getting either of this variables from an un-trusted source (such as a POST/GET) then I can make sure they are the correct data types by replacing the final line (the $query set) with a <code>sprintf</code> call like this:</p> <pre><code>$username = 'Simon'; $id = 3; $query = sprintf( "SELECT FROM `users` WHERE `id` = %d AND `name` = '%s'", $id, $username ); </code></pre> <p><code>sprintf</code> will simply not let me use a string for the $id or an integer for the $name when it is called which ensures the correct types of data are given (this gives me that little extra bit of security). IF incorrect data types ARE given then <em>I believe</em> it'll cast the variables to the requested type.</p> <p>To read more about <code>sprintf</code> visit here: <a href="http://php.net/sprintf" rel="nofollow noreferrer">http://php.net/sprintf</a></p> <p>I hope this explains enough (it's my first answer) :).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload