Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Do I have to use PHP Prepared Statements for all SQL queries?

I have only used PHP Prepared Statements for SQL insertions since now, but I wonder if I have to use Prepared Statements for all SQL queries, if so, how can I use them in the following SELECT statement?

// sql for category
$sql_category = "SELECT id as id_category, name as name_category
                FROM category";

$results = mysqli_query($conn, $sql_category);

if ($results === false) {
    echo mysqli_error($conn);
} else {
    $categories = mysqli_fetch_all($results, MYSQLI_ASSOC);
}

I would be very pleased to be correctly answered, and if you have any suggestions or questions, please let me know.

This code works properly at the moment.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

SQL injection prevention is a bonus feature of prepared statements, otherwise, they’re just "prepared statements"… prepare once, execute many times, with different parameter values.

That being said, you could use prepared statements throughout your code base for consistency, even if the query does not accept any parameters. You’ll have to:

  • use mysqli::prepare to create a mysqli_stmt object
  • use mysqli_stmt::bind_param to specify parameter values (if any)
  • use mysqli_stmt::execute to execute the statement
  • use mysqli_stmt::get_result to get the result
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading