我正在尝试使用 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);
注意: - ,我无法使用mysqli
或PDO
,因为我只是在旧项目上测试某些东西
答案 0 :(得分:2)
该函数使用所选字体在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);
注意: - 按原样添加其余代码。