Sophie Bernard
Sophie Bernard

Reputation: 207

mysql_fetch_array gets the first result only, even with while loop

I'm trying to print multiple mysql rows, using mysql_fetch_array with while loop but it only prints the first result while my table contains many of them with the right conditions

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

Note:- and I can't use mysqli or PDO as I'm just testing something on an old project

Upvotes: 0

Views: 91

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Based on printer_draw_text Manual

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

In your code x,y values are 10,10. So each time new text written on the same postion. That means previous one over-written by new-one and hense only single value is drawn.

There are two possible solution:-

1.Either Change x,y position values after each iteration.

$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.Or Create new pages in each iteration:-

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

Note:- Add rest of the code as it is.

Upvotes: 2

Related Questions