mysql\u fetch\u数组只返回一行

mrwjdhj3  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(377)

我需要将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),输出将为空。
注解字段是sql中的“text”类型。有人知道吗,为什么不能输出“text”和“date”列?

$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>)

这个代码看起来像这个截图

9w11ddsr

9w11ddsr1#

您很可能会发现它正在获取所有数据,但您只是在循环外连接它。所以把它移到循环中使用所有的数据。。。

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 (拆下 ..= )...

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

这将确保它从标题开始,然后是 .= 在每个循环中,将在每行上添加。

相关问题