首页 > 解决方案 > mysql_fetch_array 只返回一行

问题描述

我需要将我的 SQL 查询的每一行作为变量导出并使用 TCPDF 打印。目前它只返回一行。

$result = mysqli_query($con,"SELECT id,betrag,notiz,datum, CASE WHEN entnahme = '0' THEN 'Einzahlung' ELSE 'Auszahlung' END AS entnahme_text FROM bestand WHERE datum >= '2018-01-01 00:00:00' ORDER BY datum DESC;"$
$tbl .= '<tr><td>ID</td><td>Richtung</td><td>Betrag</td><td>Notiz</td><td>Datum</td></tr>';

while($row = mysqli_fetch_array($result))
  {
  $id = $row['id'];
  $entnahme = $row['entnahme_text'];
  $betrag = $row['betrag'];
  $notiz = $row['notiz'];
  $datum = $row['datum'];
}

$tbl .= '<tr><td>' . $id . '</td><td>' .$entnahme. '</td><td>' . $betrag . '</td><td>' .$notiz. '</td><td>' .$datum. '</td></tr>';

// Print text using writeHTMLCell()
$pdf->writeHTML($tbl1 . $aktuellesdatum . $date . $br  . $tbl_header . $tbl . $tbl_footer, true, false, false, false, '');

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('', 'I');

谢谢,现在我已经得到了一个有用的 pdf 输出。但是如果我添加 $notiz (note) 和 $datum (date) 输出将是空白的。

note 字段是 sql 中的“文本”类型。有谁知道,为什么它不能输出“文本”和“日期”列?

$result = mysqli_query($con,"SELECT id,betrag,notiz,datum, CASE WHEN entnahme = '0' THEN 'Einzahlung' ELSE 'Auszahlung' END AS entnahme_text FROM bestand WHERE datum >= '2018-01-01 00:00:00' ORDER BY datum DESC;");
$tbl .= '<tr><td>ID</td><td>Richtung</td><td>Betrag</td><td>Notiz</td><td>Datum</td></tr>';
$tblcontent = '';

while($row = mysqli_fetch_array($result))
  {
  $id = $row['id'];
  $entnahme = $row['entnahme_text'];
  $betrag = $row['betrag'];
  $notiz = $row['notiz'];
  $datum = $row['datum'];
  $tblcontent .= "<tr><td>$id</td><td>$entnahme</td><td>$betrag</td></tr>";
}

截屏


我试过这样。

while($row = mysqli_fetch_array($result))
  {
  $id = $row['id'];
  $entnahme = $row['entnahme_text'];
  $betrag = $row['betrag'];
  $notiz = $row['notiz'];
  $datum = $row['datum'];
  $tblcontent = '<tr><td>id</td><td>betrag</td></tr>';
}

$pdf->writeHTML($tbl1 . $aktuellesdatum . $date . $br  . $tbl_header . $tbl . $tblcontent . $tbl_footer, true, false, false, false, '');


$tbl1 is used as title in the pdf
$aktuellesdatum text for current date
$date for date
$tbl_header is used to create the table (<table border="1"><h6></h6>)
$tbl for the first description row
$tblcontent for the output of the sql-query
$tbl_footer to close the table (</table>)

使用此代码,它看起来像 这样

标签: phpmysql

解决方案


您很可能会发现它正在获取所有数据,但您只是在循环之外连接它。因此,将其移入循环以使用所有数据...

while($row = mysqli_fetch_array($result))
  {
  $id = $row['id'];
  $entnahme = $row['entnahme_text'];
  $betrag = $row['betrag'];
  $notiz = $row['notiz'];
  $datum = $row['datum'];
  $tbl .= '<tr><td>' . $id . '</td><td>' .$entnahme. '</td><td>' . $betrag . '</td><td>' .$notiz. '</td><td>' .$datum. '</td></tr>';
}

您可以摆脱临时变量并直接插入数据,这会更有效......

while($row = mysqli_fetch_array($result))
  {
  $tbl .= '<tr><td>' . $row['id'] . '</td><td>' .
         $row['entnahme_text']. '</td><td>' . $row['betrag'] . 
         '</td><td>' .$row['notiz']. '</td><td>' .$row['datum']. 
         '</td></tr>';
}

我认为您需要更改$tbl(删除.in .=)的第一行...

$tbl = '<tr><td>ID</td><td>Richtung</td><td>Betrag</td><td>Notiz</td><td>Datum</td></tr>';

这将确保它以标题开头,然后.=在每个循环中添加每一行。


推荐阅读