Optimization in PHP PDO code

Asked

Viewed 89 times

-2

hello, I’m studying a way to increase a value before storing in the database but I need help, searching Google for parts I was able to assemble the code and yes it works, goal is to update a value in the database, but I’m finding the code confusing someone could help me?

<?php

try{
$id = 1;

$pdo = new PDO("mysql:host=localhost;dbname=isbn_db", "root", "");

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//busca no banco de dados
$stmt2 = $pdo->prepare("SELECT * FROM isbn ORDER BY numero_isbn"); 
$stmt2->execute();

//fecAll busca todos os resultados
$results = $stmt2->fetchALL(PDO::FETCH_ASSOC);
//BUSCA O VALOR NO BANCO DE DADOS 
foreach ($results as $value){
    $var = $value['numero_isbn'];   
}    

//incrementa o valor obtido no banco
$var = $var + 10000000000;   

$stmt = $pdo->prepare('UPDATE isbn SET numero_isbn = :numero_isbn WHERE id = :id');
$stmt->execute(array(
    ':id' => $id,
    ':numero_isbn' => $var
));

// echo $stmt->rowCount();

echo "ISBN: ".$var;

}catch(PDOException $e){
    echo 'Error: '. $e->getMessage();
    echo "ERRO";
}

?>
  • The first thing you have to ask, why do you use PDO? What is your justification for adopting it?

  • me too Niero. I would like to know why PDO, and also the reason why he wants to increase this number in php. Gustavo? Automatic MYSQL incrementing does not solve? a RANDOM number, solves your problem ?

  • This could be done in a single query, something like UPDATE isbn SET numero_isbn = :numero_isbn + 10000000000 WHERE id = :id

  • Really not a reason to use PDO, and not the automatic increment of MYSQL does not help me, actually for this case I needed to generate this number sequential, so after a little more study I reformulated my code, can I post the code? to perhaps help someone in the future?

  • 1

    It’s getting worse. Do you think you can do a better sequence control than Mysql can? I think you need to reevaluate that.

  • Only one numero_isbn = numero_isbn + 10000000000 no update would replace all your code, but I do not claim that this is the solution, because I barely understood what you are trying to do, including the fact that maybe an ISBN value is numerical.

  • worsening? I am only asking for help with code because I started to study about it, not judging if it is better or worse than Mysql, but for the little knowledge I have at the moment I preferred to increase this value by treating it within my code, I know that in the future I will look at my code and feel shame but at the moment is what I can do, because I started to learn this yesterday, can close this my question, Anderson I did what you suggested and the code decreased a lot, with your help I managed to dry him, stay with God in that embrace.

Show 2 more comments

1 answer

1


Many programmers (including myself a few years ago) would do the following procedure to add a value to a column in the database:

  • A Select to get the current quantity
  • The addition of the value
  • Finally an Update to update column

There is a faster and more correct way to do this procedure.

    SET numero_isbn = numero_isbn + :numero_isbn

Optimization of PHP PDO code

$id = 1;
$var=10000000000;

$pdo = new PDO("mysql:host=localhost;dbname=isbn_db", "root", "");

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

stmt = $pdo->prepare('UPDATE isbn SET numero_isbn = numero_isbn + :numero_isbn WHERE id = :id');
$stmt->execute(array(
    ':id' => $id,
    ':numero_isbn' => $var
));
  • Thank you so much for your help so I can lower my code, that hug

Browser other questions tagged

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