Standardization for MySQLi use

2

I am researching on the use of MySQLi and I have seen many articles, but each one always has some particularities about the pattern of development, my question anyone already set a default? For example something that can be followed with best practices?

For example, an embed code for this article here: PHP MySqli Basic usage (select, insert & update)

real_escape_string('P1234').'"';
$product_name = '"'.$mysqli->real_escape_string('42 inch TV').'"';
$product_price = '"'.$mysqli->real_escape_string('600').'"';

//MySqli Insert Query
$insert_row = $mysqli->query("INSERT INTO products (product_code, product_name, price) VALUES($product_code, $product_name, $product_price)");

if($insert_row){
    print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'
'; }else{ die('Error : ('. $mysqli->errno .') '. $mysqli->error); } ?>

And in this other article: How to Use PHP Improved MySQLi extension

$v1="'" . $conn->real_escape_string('col1_value') . "'";

$sql="INSERT INTO tbl (col1_varchar, col2_number) VALUES ($v1,10)";

if($conn->query($sql) === false) {
  trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
  $last_inserted_id = $conn->insert_id;
  $affected_rows = $conn->affected_rows;
}
    
asked by anonymous 04.03.2015 / 20:09

1 answer

2

Currently the most used and secure PHP libraries are PDO and MySQLI

PDO has the advantage of being adaptive to many databases without having to change all your code.

MySQLI is great for projects that use only MySQL as a database and do not aim to use others.

In any case both are safe and give the message of small and large projects.

On the pattern, there is no universal thing. It goes from its necessidades and its gosto

    
04.03.2015 / 21:39