2016-09-17 6 views
1

いくつかのフィールドを合計して数えるPHPを介して条件付きSQLクエリを生成する必要があります。それをカットして、私が書いたクエリに直接行きましょう。数と合計データよりも SQL SELECT複数のフィールドをカウントする

$query_voucher="SELECT *, 
count(case WHEN IDprestazione=0 then 1 else null end) as quantitaliberi, 
count(case WHEN IDprestazione != 0 AND pagato=0 then 1 else null end) as quantitaprenotati, 
count(case WHEN pagato=1 then 1 else null end) as quantitapagati, 
count(*) as quantita, 
sum(valorelordo case WHEN IDprestazione=0 then 1 else 0 end) as valoreliberi, 
sum(valorelordo case WHEN IDprestazione=1 AND pagato=0 then 1 else 0 end) as valoreprenotati, 
sum(valorelordo case WHEN pagato=1 then 1 else 0 end) as valorepagati, 
sum(*) as valore 
FROM voucher"; 
$conditions=array(); 

if (!empty($IDpersona_recuperato) && $IDpersona_recuperato!="nessunvalore") { 
     $conditions[]="IDprestazione IN (SELECT IDprestazione FROM prestazioni WHERE IDpersona=$IDpersona_recuperato)"; 
} 
if (!empty($cerca_idprestazione)) { 
    $conditions[]="IDprestazione='$cerca_idprestazione'"; 
} 
if (!empty($dataemissione)) { 
    $conditions[]="dataemissione <= '$dataemissione'"; 
} 
if (!empty($valore)) { 
    $conditions[]="valorelordo = '$valore'"; 
} 
if (!empty($codicecontrollo)) { 
    $conditions[]="codice = '$codicecontrollo'"; 
} 
if (!empty($numero)) { 
    $conditions[]="numero = '$numero'"; 
} 
if ($consegnato=="si"){ 
    $conditions[]="consegnato=1"; 
}elseif ($consegnato=="no") { 
    $conditions[]="consegnato=0"; 
} 

if (count($conditions) > 0) { 
    $query_voucher .=" WHERE " . implode(' AND ', $conditions); 
} 

if ($ordinaper=="numero") { 
    $query_voucher .=" ORDER BY numero"; 
}elseif ($ordinaper=="idprestazione") { 
    $query_voucher .=" ORDER BY IDprestazione"; 
}elseif ($ordinaper=="valore") { 
    $query_voucher .=" ORDER BY valorelordo DESC"; 
}elseif ($ordinaper=="consegnato") { 
    $query_voucher .=" ORDER BY consegnato"; 
} 

この

while ($row_voucher=mysql_fetch_row($risultato_query_voucher)) { 
if ($row_voucher[1]!=0){ 
    $risultato_query_prestazioni=mysql_query("SELECT * FROM prestazioni WHERE IDprestazione='$row_voucher[1]'"); 
} 

    echo "<tr> 
      <th><div class='showdata'>+</div><div style='margin-top:-20px;margin-left:15px;'><input type='checkbox' name='IDvoucher' id='IDvoucher' value='" . $row_voucher[0] . "'></div></th> 
      <td>" . $row_voucher[2] . "</td> 
      <td>" . $row_voucher[3] . "</td> 
      <td>" . date_format(new DateTime($row_voucher[4]), 'd/m/Y') . "</td> 
      <td>" . $row_voucher[6] . "€</td> 
      <td>"; if ($row_voucher[1]==0){echo "<font color=green>Libero</font>";}else{echo "$row_voucher[1]";}echo "</td> 
      <td>";if ($row_voucher[7]==0) { 
       echo "No"; 
      }else{ 
       echo "Si"; 
      } echo "</td> 
      <td>";if ($row_voucher[7]==1){echo date_format(new DateTime($row_voucher[8]), 'd/m/Y');} echo "</td> 
     </tr>"; 
のように別のテーブルに、より一般的なデータを取得するために使用されるクエリで *より

$row_voucher1=mysql_fetch_row($query_voucher); 

echo "<form method='POST' name='elimina' id='elimina' action='php/elimina_voucher.php'> 
    <table class='table table-striped' style='margin-top:70px; width: 60%;'> 
    <caption>Riassunto Query Eseguita</caption> 
    <tr> 
     <th></th> 
     <th>liberi</th> 
     <th>prenotati</th> 
     <th>consegnati</th> 
     <th>Totale</th> 
    </tr>"; 
echo "<td> 
     <tr>" . $row_voucher1[quantitaliberi] . "</tr> 
     <tr>" . $row_voucher1[quantitaprenotati] . "</tr> 
     <tr>" . $row_voucher1[quantitapagati] . "</tr> 
     <tr>" . $row_voucher1[valoreliberi] . "</tr> 
     </td>"; 

    echo "</table>"; 

のようにテーブルに移動しますsumcountを使用する前に

とクエリが単純にSELECT * FROM voucherでした。2番目のテーブルのすべてが正常に動作していました。 今私はかなり一般的なエラーWarning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /volume1/web/voucher/index6.php on line 285何か間違っている(そしてそこだけではない)というサインを得ました。

何か助けていただければ幸いです。

+0

ログ全体のクエリをmysql dpで実行し、エラー報告 – siddhesh

+0

@siddheshを実行できません。問題がクエリ内にあることがわかっているので、それを確認できますか? – TheMerovingian

+0

@siddhesh ehm ...どうか? – TheMerovingian

答えて

0

mysql_fetch_arrayに渡す前に$ resultをチェックしてください。クエリが失敗したため、これは誤っていることがわかります。戻り値の可能性については、mysql_queryのマニュアルを参照してください。

$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'"); 

if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling 
} 

while($row = mysql_fetch_array($result)) 
{ 
    echo $row['FirstName']; 
} 
+0

私はクエリが失敗したことを知っています。私は '[...]クエリーに何か間違ったサインを書いています。しかし、基本的には私が検索した情報が非常に曖昧であるため、mysql_errorを使っても何が問題なのかを知る必要があります。 – TheMerovingian

関連する問題