mysql_fetch_array只获取第一个结果,即使是while循环也是如此

时间:2018-01-16 11:19:25

标签: php mysql arrays while-loop

我正在尝试使用 mysql_fetch_array while循环打印多个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,因为我只是在旧项目上测试某些东西

1 个答案:

答案 0 :(得分:2)

基于printer_draw_text Manual

  

该函数使用所选字体在x,y位置绘制文本。

在您的代码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);

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