Multiquery PHP PDO - SQL Server 2012 - IIS 8.5

Asked

Viewed 109 times

2

Is there a problem with 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 a proper way to execute multiple queries with the same command?

  • One of the problems is sql Injection (so by default only one query is sent), already imagined how to handle two result sets at the same time?

  • 1

    I confused, who has this behavior (send only a query mysqli_query(), more than one mysqli_multi_query()) is Mysqli. Has a related Soen

  • That solves it. Thanks, @rray!!!

1 answer

0


Not to leave the question unanswered.

No problem in running many darlings with PDO, but you have to be careful, especially when you’re expecting input of users.

An alternative form to the use of multiple darlings are the stored procedures in the database.

Details:

To run multiple queries at the same time you need PHP to be version 5.3 or higher and Prepared statements emulated (using the flag PDO::ATTR_EMULATE_PREPARES).

Thus:

$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()
        )
    );
}

Browser other questions tagged

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