UPDATED PHP
<?php
$result = $sth->fetchAll();
print_r($result); //or 开发者_JS百科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
Output of Louis Code:
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.
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/>";
}
}
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.
精彩评论