Multiquery PHP PDO - SQL Server 2012 - IIS 8.5

2

Is there a problem in running multiple queries with the same PHP PDO code?

Example:

<?php

// Retorna $con
include 'conexao.php';

// Suponhamos que dbo.minha_tabela tenha apenas três campos, sendo um deles a chave-primária auto incrementável
$query = "update minha_tabela set coluna = 'valor_da_coluna' where coluna = 'condicao'; ";

$query .= "insert into minha_tabela values('valor_da_coluna', 'valor_da_coluna2'); ";

try{
    $prepare = $con->prepare($query);
    $prepare->execute();

    echo json_encode(array('status' => '200 OK', 'message' => 'query executada com sucesso'));
} catch(\Exception $ex){
    echo json_encode(
        array(
            'descrition' => $ex->getMessage(),
            'code' => $ex->getCode()
        )
    );
}

Is there an appropriate way to run multiple queries with the same command?

    
asked by anonymous 11.01.2017 / 19:04

1 answer

0

Not to leave the question unanswered.

There's no problem running many queries with PDO , but you have to be cautious, especially when expecting input from users.

An alternate way to use multiple queries is the

in> stored procedures in the database.

Details:

To execute multiple queries at the same time, PHP must be version 5.3 or higher and prepared statements emulated (using the PDO::ATTR_EMULATE_PREPARES flag).

This way:

$con->setAttribute(PDO::ATTR_EMULATE_PREPARES);

try{
    $prepare = $con->prepare($query);
    $prepare->execute();

    echo json_encode(array('status' => '200 OK', 'message' => 'query executada com sucesso'));
} catch(\Exception $ex){
    echo json_encode(
        array(
            'descrition' => $ex->getMessage(),
            'code' => $ex->getCode()
        )
    );
}
    

15.03.2017 / 23:50