簡體   English   中英

PHP,MYSQL 僅獲取結果數組的第一行和第二行

[英]PHP, MYSQL get 1st and second row only of result array

我有一個查詢結果是這樣的。

SELECT check.c_id, check.voucher, check.payee, check.c_date, transact.atc_code, transact.value
    FROM `check`
    LEFT JOIN `transact`
    ON check.c_id = transact.t_id
    WHERE t_id='1' AND (atc_code LIKE '%72%' OR atc_code LIKE '%35%')
    ORDER BY check.c_id
"c_id"   "voucher"      "payee"                     "c_date"        "atc_code"      "value"
"1"      "PDMK162953"   "TOYOTA GLOBAL CITY, INC."  "04/11/2016"    "MK-GF-7202"    "10338.44"
"1"      "PDMK162953"   "TOYOTA GLOBAL CITY, INC."  "04/11/2016"    "MK-GF-3505"    "206.77"
while($row = $result->fetch_assoc()) {
    $c_id = $row['c_id'];
    $voucher = $row['voucher'];
    $payee = $row['payee'];
    $c_date = strtotime($row['c_date']);
    $atc_code = $row['atc_code'];
    $income = $row['value'];
    $tax = $row['value'];
}

我將如何將列“值”回顯到 html 表格的另一個單元格中的第一個單元格 10338.44 和 206.77

只需在您的查詢中提及限制。

SELECT check.c_id, check.voucher, check.payee, check.c_date, transact.atc_code, transact.value
FROM `check`
LEFT JOIN `transact`
ON check.c_id = transact.t_id
WHERE t_id='1' AND (atc_code LIKE '%72%' OR atc_code LIKE '%35%')
ORDER BY check.c_id
LIMIT 2    // Add this

在一行和兩列中顯示記錄:

 <table border="1">
   <tr>
     <th>Tax 1</th>
     <th>Tax 2</th>
     <?php  while($row = $result->fetch_assoc()) { ?>
     <td>
       <?php  echo $row['value']; ?>
     </td>
     <?php } ?>
   </tr>
 </table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM