-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

11
  • Are there more rows to fetch? What is var_dump($query)? Commented Dec 11, 2018 at 9:19
  • your code is ok. can you show your desired output. Commented Dec 11, 2018 at 9:25
  • $res does exist inside loop because it is declared in line 3. Commented Dec 11, 2018 at 9:26
  • Doesn't $rows[] = $res['product'] create a numerical index? Your print_r($rows) output doesn't look like that. How can that print_r be the result of your loop? Commented Dec 11, 2018 at 9:46
  • sorry that was the output form something someone requested i just forgot to remove the print_r reference Commented Dec 11, 2018 at 9:48

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

15 Comments

still the same @Joseph_J
It appears that your query is only returning 1 product.
Yeah which is annoying because there are 36000 records If i do a simple echo $res['product']; inside the do while loop it returns them all
See Updated answer.
cheers :) stupid encoding issue ..
|
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;

8 Comments

Am getting Warning: Illegal string offset 'product' in line 21 thats the $rows[] = $val['product']; line. Will try your last suggestion now
You are getting it because you don't have a key named product in your array.
have just added the output you requested
did you try the changes I updated in answer?
yeah the last suggestion you made just outputted 1 product
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.