-1

This is puzzling the hell out of me .. Simple fetch & loop isn't displaying any data There is data in the table as displaying the result outside the loop returns data

$dbc = mysqli_connect($dbh, $dbu, $dbp, $dbn);
$query = mysqli_query($dbc,"SELECT * FROM `products`");
$res = mysqli_fetch_assoc($query);
$rows = array();
do {
    $rows[] = $res['product'];

}while($res = mysqli_fetch_assoc($query)) ;
$data = json_encode($rows);
echo $data;
echo mysqli_error($dbc);

ADDED :

returns the data in the array

<pre>Array
(
    [ID] => 1
    [list_name] => Dry Fruits
    [cat] => Dry Fruits and Nuts
    [item_code] => 1049
    [pack_style] => 106
    [packstyle_active] => Y
    [style_desc] => Stabilo250
    [system_wt] => 0.25
    [weight_desc] => 250gm
    [declared_weight] => 0.25
    [declared_weight_desc] => 250gm
    [uom] => gm
    [qty_in_case] => 1
    [case_size] => 1 x 250gm
    [product] => Almond Sliced
    [weight_code] => 126
    [qty_code] => 00
    [product_code] => 1049-106-126-00
    [barcode] => 502185878567
    [barcode13] => 5021858785678
    [sequence_no] => 78567
    [sage_code] => 104978567
    [active] => N
    [allergen] => Y
    [origin] => Various
    [density] => 0
    [processtype] => PT32
    [processed_by] => FGS
    [Industrial] => N
    [Catering] => N
    [RetailEthnic] => Y
    [RetailMainstream] => N
    [Website] => N
    [Special] => N
    [zohoitemcode] => 1540890000001353073
)

So I think its an issue with the json_encode.. maybe

    print_r($rows) 
    Array
   (
    [0] => Almond Sliced
    [1] => Almond Sliced
    [2] => Almond Sliced
    [3] => Almond Sliced
    [4] => Almond Sliced
    [5] => Almond Sliced
    [6] => Almond Sliced
    [7] => Almond Sliced
    [8] => Almond Sliced
    [9] => Almond Sliced
    [10] => Almond Sliced
    [11] => Almond Sliced
    [12] => Almond Sliced
    [13] => Almond Sliced
    [14] => Almond Sliced
    [15] => Almond Sliced
    )

There is over 36000 records of products

Chris Yates
  • 243
  • 3
  • 16

2 Answers2

2

This appeared to be a strange encoding issue. Added a line in the while loop to force the encoding to UTF-8.

$dbc = mysqli_connect($dbh, $dbu, $dbp, $dbn);
$query = "SELECT * FROM `products`";
$stmt = $dbc->prepare($query);
$stmt->execute();
$results = $stmt->get_result();

while($res = $results->fetch_assoc()){

  $rows[] = mb_convert_encoding($res['product'], 'UTF-8', 'UTF-8');

}

echo '<pre>';
print_r($rows);
echo '</pre>';

$data = json_encode($rows);
echo $data;

echo mysqli_error($dbc);
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
0

Try change your loop to the following:

foreach($res as $val) {
    $rows[] = $val['product'];
}

Moreover, if it still doesn't work. then:

echo "<pre>";
print_($res);
die(); 

and please post the output.

Edit: 1

You don't need foreach loop.

you can directly do:

$data = json_encode($res);
echo $data;
oreopot
  • 3,392
  • 2
  • 19
  • 28