2016-05-30 3 views
0

このコードを使用してデータベースから行を取得します。プリペアドステートメントからの結果を確認するPDO?

// Prepare WC statement 
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = :idQuotation"); 
// Execute Unit prices statement 
$queryUP->execute(array(
'idQuotation' => $idQuotation 
)); 

// How to check the results is empty or not ? 
if (results) { 
    // foreach($queryUP as $rowup) { 
     //... 
    // } 
} else { 
    // do another thing 
} 

コードを続行する前にクエリに結果があるかどうか確認する方法はありませんか?

答えて

2
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = ?"); 
$queryUP->execute(array($idQuotation)); 

//here you go 
$results = $queryUP->fetchAll(); 

if ($results) { 
    // foreach($results as $rowup) { 
     //... 
    // } 
} else { 
    // do another thing 
} 

ここに示すように、テンプレート内でforeachを行っていて、適切な場所に配置されていないとします。

+0

それは場合にforeachのを行うには間違っているのはなぜ? – wawanopoulos

+0

ループ内に何かをエコーし​​ている場合は、テンプレートは使用されていないことを意味します。 –

+0

あなたのテンプレートは何ですか? – wawanopoulos

0

あなたの記憶は、あなたが許可している場合、あなたはすべてを取得することができます

$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = :idQuotation"); 
$queryUP->bindParam(':idQuotation', $idQuotation, PDO::PARAM_STR); 
$queryUP->execute(); 
$result = $queryUP->fetchAll(PDO::FETCH_ASSOC); 
if (count($result) > 0) { 
} else { 
} 
関連する問題