Standardization for Mysqli use

Asked

Viewed 265 times

2

I’m researching the use of Mysqli and have seen many articles, but each one always has some particularities about the development pattern, my question has anyone defined a pattern? For example something that can be followed with best practices?

For example an insert code from 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 that 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;
}
  • Ferinha, I am more PDO-compliant to Mysqli and PSR-compliant.. Another thing, in what concerns design partern, in my last developed applications, I thought it necessary to use Active Record, there is a lot in google about it

  • see this video: https://www.youtube.com/watch?v=OGzK1uFBJd0 I think you can help...

  • 1

    Pattern in which direction and than?

  • Hello @rray, thanks for asking, how to follow a pattern for CRUD, I have read many articles and always have a lot of different things.

  • Post examples of this difference in articles

  • Your question is quite incomplete. State what pattern you are saying and which usage finality

Show 1 more comment

1 answer

2


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

The PDO has the advantage of being adaptive to several databases without the need to change all their code.

Already the Mysqli is excellent for projects that use only Mysql as a database and are not intended to use other.

In any case both are safe and can handle small projects as large.

About the pattern, there is no universal thing. It goes from your necessidades and your gosto

Browser other questions tagged

You are not signed in. Login or sign up in order to post.