如何同时运行这两个查询?

时间:2011-09-19 23:54:10

标签: php mysql while-loop prepared-statement

更新PHP

    <?php 
    $result = $sth->fetchAll();
    print_r($result); //or var_dump($result); for more info
    foreach($result as $row){
        print_r($row);
    }   
    ?>  

在SQL视图上:

$pdo = new PDO($h1, $u, $p);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
SELECT uFName, uLName, listTitle, listPropPrice, listCmt, listDt
FROM User U, Listing L
WHERE U.uID = L.uID
;');
$sth->execute(array());

#GET Merchant (Seller) Info and Listings Offered On
$pdo2 = new PDO($h1, $u, $p);
$pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth2 = $pdo2->prepare('
SELECT mFName, mLName, moAmt, moDtOff
FROM Merchant M, MerchantOffer MO, Listing L
WHERE M.mID = MO.mID 
AND L.listID = MO.listID
;');
$sth2->execute(array());

如何在同一$sth语句中运行$sth2WHILE

SCHEMA imggg

路易码的输出: kfgdfgdfgdf

3 个答案:

答案 0 :(得分:1)

您不必同时运行查询。

事实上,从数据库获取数据已经(或应该)与创建HTML无关。它的不同任务。

因此,首先将数据放入数组中,然后以任何方式将其打印出来 - 同时或格式化。

答案 1 :(得分:0)

我认为你正在寻找的是fetchAll语句,然后你有一个结果数组在使用另一个时同时迭代。 (你需要跟踪一个单独的计数器)

更新:

现在我知道了,试试这个。

 //Get the listing info
  $pdo = new PDO($h1, $u, $p);
  $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  $sth = $pdo->prepare('
    SELECT uFName, uLName, listTitle, listPropPrice, listCmt, listDt, listID
    FROM User U, Listing L
    WHERE U.uID = L.uID
  ;');
  $sth->execute(array());

  $listings = $sth->fetchAll();

  //loop through all the listings
  foreach($listings as $listing){

    echo "Listing info...<br/>";

    //grab the merchant info for the listing
    $pdo2 = new PDO($h1, $u, $p);
    $pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sth2 = $pdo2->prepare("
      SELECT mFName, mLName, moAmt, moDtOff
      FROM Merchant M, MerchantOffer MO, Listing L
      WHERE M.mID = MO.mID 
      AND L.listID = MO.listID
      and L.listID = {$listing['listID']}
    ;");
    $sth2->execute(array());

    //if there is only one merchant per listing, probably don't 
    //need fetchAll and can go back to single row
    $merchants = $sth2->fetchAll();
    //loop through all merchants
    foreach($merchants as $merchant){
      echo "Merchant info...<br/>";
    }
  }

答案 2 :(得分:0)

我已更新路易斯代码,因为我认为它可以更好地处理。

//Get the listing info
$pdo = new PDO($h1, $u, $p);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
  SELECT uFName, uLName, listTitle, listPropPrice, listCmt, listDt, listID
  FROM User U, Listing L
  WHERE U.uID = L.uID');
$sth->execute();

$listings = $sth->fetchAll();

//Prepare the statement to grab the merchant info for the listing
$pdo2 = new PDO($h1, $u, $p);
$pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth2 = $pdo2->prepare('
  SELECT mFName, mLName, moAmt, moDtOff
  FROM Merchant M, MerchantOffer MO, Listing L
  WHERE M.mID = MO.mID 
  AND L.listID = MO.listID
  and L.listID = :listid');

$sth2->bindParam(':listid', $listing['listID'], PDO::PARAM_INT);

//loop through all the listings
foreach($listings as $listing){

    echo "Listing info...<br/>";

    // Executed prepared statement (the parameter is updated automatically)
    $sth2->execute();

    //if there is only one merchant per listing, probably don't 
    //need fetchAll and can go back to single row
    $merchants = $sth2->fetchAll();
    //loop through all merchants
    foreach($merchants as $merchant){
       echo "Merchant info...<br/>";
    }
}

变化是:

  • 使用bindParam设置值(如果使用PDO,最好使用它,或者使用bindValue)。
  • 只制作一个预先准备好的语句来优化它,而不是每次迭代都有一个。
  • 删除';'从sql,php手册说sql句子必须没有它。

我无法评论,因为我已经做了一个新的答案,而不是评论或编辑路易斯答案。