2016-05-30 76 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 { 
}