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

发布于 2024-12-05 13:15:33 字数 1142 浏览 4 评论 0原文

更新的 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$sth2 WHILE 语句?

模式 imggg

Louis 代码的输出: kfgdfgdfgdf

UPDATED PHP

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

On the SQL View:

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

How do I run $sth and $sth2 within the same WHILE STATEMENT?

SCHEMA
imggg

Output of Louis Code:
kfgdfgdfgdf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-12-12 13:15:33

您不必同时运行查询。

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

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

you don't have to run your queries simultaneously.

As a matter of fact, getting data from database has (or should be) nothing to do with creating HTML. Its different tasks.

So, get your data into array(s) first and then print it out in whatever manner you wish - simultaneously or checkered.

与君绝 2024-12-12 13:15:33

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

更新:

现在我明白了,试试这个。

 //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/>";
    }
  }

I think what you're looking for is the fetchAll statement, you then have a result array to iterate through at the same time of using the other while. (you'll need to keep track of a separate counter)

UPDATE:

Now I get ya, try this.

 //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/>";
    }
  }
断桥再见 2024-12-12 13:15:33

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

//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语句一定是没有它的。

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

I have update Louis code because I think it can better handle.

//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/>";
    }
}

The changes are:

  • Use bindParam to set the values (if you use PDO it's better to use it, or bindValue).
  • Only make one prepared statement to optimize it, and not one for each iteration.
  • Remove ';' from sql, php manual say that sql sentence must be without it.

I can't comment, because it I have made a new answer instead of comment or edit Louis answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文