php - mysql_fetch_array 仅获取第一个结果,即使使用 while 循环

标签 php mysql arrays while-loop

我正在尝试使用mysql_fetch_arraywhile循环来打印多个mysql行,但它只打印第一个结果,而我的表包含许多具有正确条件的结果

$queryString="SELECT * FROM items WHERE order_id='".$order_id."' and confirm_order='0' ORDER BY id";
$myquery=mysql_query($queryString);

$handle = printer_open("POS");
printer_start_doc($handle, "My Document");
printer_start_page($handle);
$font = printer_create_font("Arial", 35, 20, 300, false, false, false, 0);
printer_select_font($handle, $font);

while ($fetch = mysql_fetch_array($myquery)) {

    $product=$fetch[product_name];
    $type=$fetch[type];
    $q=$fetch[item_quantity];
    $comment=$fetch[comment];

    $tex="".$q." ".$type." ".$comment." ".$product."";
    printer_draw_text($handle, $tex, 10, 10);
  }
printer_delete_font($font);
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);

注意:-并且我不能使用mysqliPDO,因为我只是在测试一些东西在一个旧项目上

最佳答案

基于printer_draw_text Manual

The function draws text at position x, y using the selected font.

在您的代码中,x,y 值为 10,10。所以每次新的文字都写在同一个位置上。这意味着前一个被新的覆盖,因此只绘制单个值。

有两种可能的解决方案:-

1.每次迭代后更改 x,y 位置值。

$counter = 10; //add a number counter

while ($fetch = mysql_fetch_assoc($myquery)) {//use assoc for lighter array iteration

    $product=$fetch['product_name']; //use quotes around indexes, best practise
    $type=$fetch['type'];
    $q=$fetch['item_quantity'];
    $comment=$fetch['comment'];

    $tex="$q $type $comment $product";//remove unncessary quotes
    printer_draw_text($handle, $tex, $counter, $counter); // use that number counter as x,y position
    $counter+10;//in each iteration chnage the value by adding 10
}

2.或者在每次迭代中创建新页面:-

while ($fetch = mysql_fetch_assoc($myquery)) {//use assoc for lighter array iteration

    $product=$fetch['product_name'];//use quotes around indexes, best practise
    $type=$fetch['type'];
    $q=$fetch['item_quantity'];
    $comment=$fetch['comment'];

    $tex="$q $type $comment $product";//remove unncessary quotes
    printer_draw_text($handle, $tex, 10, 10);
    printer_end_page($handle); //end page on each iteration
  }
printer_delete_font($font);
printer_end_doc($handle);
printer_close($handle);

注意:- 按原样添加其余代码。

关于php - mysql_fetch_array 仅获取第一个结果,即使使用 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48280037/

相关文章:

Python mysql 是准备好的语句还是我需要 escape_string()?

mysql - MySql WorkBench 中缺少数据库表列

php - MySQL 的 CHAR 数据类型是否有可以存储超过 255 个字符的固定长度版本?

arrays - 如何在 Julia 中实现对二维数组的洪水填充?

c - 从二维数组中找到最大值,并将最大值之前的所有值相加,然后将最大值之后的所有值相乘

javascript - 如果 Javascript 中不存在某个项目,则跳过而不出现错误

php - curl 错误 60 : SSL certificate: unable to get local issuer certificate - DigiCert

javascript - JQuery 访问 PHP 生成的内容

php - 将自定义帖子类型/帖子添加到 Woocommerce

php - 在 MVC 中处理片段和 View