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

1

Some way to shorten this script ?? Or every time I make a INSERT in the database using PDO and the bindValue function, will I have to write row by line ?? Or you can use an array or something faster and easier.

//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);
    
asked by anonymous 04.02.2016 / 16:27

1 answer

2

bindValue() or bindParam() pass only a value to bind, the alternative is the execute() that allows to receive an array, the keys being the name of the identifiers or number in the case of the 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'));
    
04.02.2016 / 17:08