开发者

Problem with 2 simultaneous connection with PDO/Mysql

开发者 https://www.devze.com 2023-03-18 00:47 出处:网络
Here is my simplified code: $connexion = new PDO(SQL_DSN,SQL_USERNAME,SQL_PASSWORD); $connexion2 = new PDO(SQL_DSN2,SQL_USERNAME2,SQL_PASSWORD2);

Here is my simplified code:

$connexion = new PDO(SQL_DSN,SQL_USERNAME,SQL_PASSWORD);
$connexion2 = new PDO(SQL_DSN2,SQL_USERNAME2,SQL_PASSWORD2);

[...]

$sqlIndex = "SELECT index_key,index_platforms_code
             FROM   index
             WHERE  index_missions_id = :mission";
$initFiches = $connexion->prepare($sqlIndex);
$initFiches->bindParam(":mission" , $_GET['mission']);
$initFiches->execute();
try 
{
    while ($fiche = $initFiches->fetch(PDO::FETCH_ASSOC))
    {
        print_r($fiche);

   开发者_运维问答     foreach ($structure['champs'] as $masterChamp)
        {
            //$stmt = $connexion2->prepare($masterChamp['sql']);
        }
    }
}
    catch (exception $e)
    {
        echo "error".$e->getMessage();

    }

My output:

Array
(
    [index_key] => 1
    [index_platforms_code] => 1
)
Array
(
    [index_key] => 2
    [index_platforms_code] => 2
)
Array
(
    [index_key] => 3
    [index_platforms_code] => 3
)
Array
(
    [index_key] => 4
    [index_platforms_code] => 4
)

All Right, but if I uncomment this line

$stmt = $connexion2->prepare($masterChamp['sql']);

in the foreach, this line broke the while above and here is the new output:

Array
(
    [index_key] => 1
    [index_platforms_code] => 1
)

Someone have an idea?


Here's a solution that doesn't involve 2 connections and that's more optimized.

$connexion = new PDO(SQL_DSN,SQL_USERNAME,SQL_PASSWORD);
$sqlIndex = "SELECT index_key,index_platforms_code
             FROM   index
             WHERE  index_missions_id = :mission";

$initFiches = $connexion->prepare($sqlIndex);

$initFiches->bindParam(":mission" , $_GET['mission'], PDO::PARAM_STR);

$initFiches->execute();

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $connexion->prepare("SELECT ...."); // Prepare outside of the loop, you don't have to prepare it X times, once is enough.

if(sizeof($data))
{
    foreach($data as $row)
    {
        // Do your statement binding / executing.    
    }
}

Alternatively, it seems you might be able to do this with table joining rather than issuing a query for each row you get from the 1st table. Since you haven't posted the structure, I guess there's no much helping it at the moment.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号