我正在尝试使用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
,因为我只是在测试一些东西在一个旧项目上
最佳答案
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/