How to reduce the INSERT script in the BD with PDO -> bindValue?

Asked

Viewed 666 times

1

Some way to shorten that script?? Or every time I go INSERT in the data set using PDO and the function bindValue, will I have to write line by line?? Or da para utilizar um array ou algo fácil e rápido.

//Prepara o cadastro
    $lc_reg = $pdo->prepare("INSERT INTO lc_users(u_username,u_email,u_pass,u_nome,u_sobrenome,u_dia,u_mes,u_ano)VALUES(:user,:email,:pass,:nome,:sobrenome,:dia,:mes,:ano,:sex)");
    $lc_reg->bindValue(":user",$reg_user);
    $lc_reg->bindValue(":email",$reg_email);
    $lc_reg->bindValue(":pass",$reg_senha);
    $lc_reg->bindValue(":nome",$reg_nome);
    $lc_reg->bindValue(":sobrenome",$reg_sobrenome);
    $lc_reg->bindValue(":dia",$reg_dia);
    $lc_reg->bindValue(":mes",$reg_mes);
    $lc_reg->bindValue(":ano",$reg_ano);
    $lc_reg->bindValue(":sex",$reg_sex);

1 answer

2


bindValue() or bindParam() pass only one value to be made bind, the alternative is on account of the execute() which allows receiving an array, the keys being the names of the indenters or number in the case of queries.

$stmt = $pdo->prepare("INSERT INTO tabela(campo1, campo2, campo3)VALUES(:valor1, :valor2, :valor3)");
$stmt->execute(array("valor1" => 1, "valor2" => 2, "valor3" => 3));

Or

$stmt = $pdo->prepare("INSERT INTO tabela(campo1, campo2, campo3)VALUES(?,?,?)");
$stmt->execute(array('a1', 'b2', 'c3'));

Browser other questions tagged

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