mysql_fetch_array仅返回一行

时间:2018-12-29 13:26:52

标签: php mysql

我需要将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(注释)和$ datum(日期),输出将为空白。

注释字段在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>";
}

Screenshot


我已经这样尝试过了。

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

使用此代码看起来像这样 Screenshot

1 个答案:

答案 0 :(得分:2)

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

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>'; )...

.=

这将确保它以标头开头,然后每个循环中的Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged Dim n As Integer Dim numbers As New List(Of Integer) For Each strValue As String In TextBox1.Text.Split(",".ToCharArray, StringSplitOptions.RemoveEmptyEntries) If Integer.TryParse(strValue, n) Then numbers.Add(n) End If Next Label1.Text = "Numbers: " & numbers.Count ' ... optionally do something with the values in "numbers" ... Label2.Text = "Values: " & String.Join(",", numbers.ToArray) Dim sum As Integer For Each num As Integer In numbers sum = sum + num Next Label3.Text = "Sum: " & sum End Sub 将添加到每一行。