如果在MYSQL表(PHP)中找不到数据,写一些东西

时间:2014-10-29 15:43:01

标签: php mysql

我有简单的脚本来搜索数据库中的东西。如果在表中没有找到任何结果,则应写下“Neni k dispozici”。我的问题是,如果我搜索不在数据库中的东西,它会给我一个空白的结果。所以我试着写条件如果(空($行))...但我从来没有得到这个结果,我只是再次被删空页面。我的第二个想法是,好吧,它可能不是空的。所以我尝试var_dump($ row)和print_r,但无论如何我都没有得到任何结果。空白的页面...但是,如果我尝试搜索数据库中的东西我得到了很好的var_dump所以没有阻止这个

我的代码如下:

                 try{
                    $sql = "SELECT * FROM axnmrs_calculations WHERE case_id = :case AND country = :country ORDER BY calculation_id DESC LIMIT 1";
                    //$sql = "SELECT * FROM axnmrs_calculations WHERE case_id = :case LIMIT 1";
                    $stmt_calculations = $con->prepare( $sql );
                    $stmt_calculations->bindValue( "case", $this->case, PDO::PARAM_STR );
                    $stmt_calculations->bindValue( "country", $this->country, PDO::PARAM_STR );
                    $stmt_calculations->execute();                   
                    while ($row = $stmt_calculations->fetch()){ 
                    print_r($row);
                    if (empty($row)){
                        echo  "<dt>Údaje ke kalkulaci</dt>";                            
                        echo  "<dd>Neni k dispozici</dd>";} 
                    else{                         
                      $this->calculationid = $row['calculation_id'];

                      echo  "<dt>Cena náhradních dílů</dt>";
                      $spare_parts = floatval($row['spareparts']); // Convert do  FLOAT (kôli number_format funkcii)
                      $spare_parts = number_format($spare_parts, 2, ',', ' ');
                      echo  "<dd>".$spare_parts."</dd>";
                      //echo  "<dd>".$row['spareparts']."</dd>";

                      echo  "<dt>Celkové náklady</dt>";
                      $totalcosts = floatval($row['totalcosts']); // Convert do  FLOAT (kôli number_format funkcii)
                      $totalcosts = number_format($totalcosts, 2, ',', ' ');
                      echo  "<dd>".$totalcosts."</dd>";                          
                      //echo  "<dd>".$row['totalcosts']."</dd>";                           

                      echo  "<dt>Celkový počet hodin práce</dt>";
                      if (empty($row['laborhours'])){
                      echo  "<dd>Neni k dispozici</dd>";} 
                      else                         
                      echo  "<dd>".$row['laborhours']."</dd>";

                      echo  "<dt>Datum vytvoření kalkulace</dt>";
                      if (empty($row['calculationdate'])){
                      echo  "<dd>Neni k dispozici</dd>";} 
                      else                             
                      echo  "<dd>".substr($row['calculationdate'], 0, 10)."</dd>";      // ODSTRANIT 0000

                      echo  "<dt>Lakovací materiál</dt>";
                      if (empty($row['paintmaterial'])){
                      echo  "<dd>Neni k dispozici</dd>";} 
                      else{   
                      $paintmaterial = floatval($row['paintmaterial']); // Convert do  FLOAT (kôli number_format funkcii)
                      $paintmaterial = number_format($paintmaterial, 2, ',', ' ');
                      echo  "<dd>".$paintmaterial."</dd>"; }                           
                      //echo  "<dd>".$row['paintmaterial']."</dd>";

                      echo  "<dt>Měna</dt>";
                      if (empty($row['currency'])){
                      echo  "<dd>Neni k dispozici</dd>";} 
                      else  
                      echo  "<dd>".$row['currency']."</dd>";  
                      }                                                  
                      }

                        }catch(PDOExeption $e){
                        echo $e->getMessage();
                        echo $con->errorInfo();
                        } 

有人可以告诉我如何检查数据是否在数据库中,如果没有,我会写一些错误消息?

由于

1 个答案:

答案 0 :(得分:0)

您可以询问返回多少行,并根据它添加一些错误处理..

if ($statement->execute()) {
    $statement->store_result();
    if ($statement->num_rows >= "1") {
         while ($statement->fetch()) {
         }
    }
}