这让我感到困惑。 简单的提取和循环不显示任何数据 表中有数据,因为在循环外显示结果将返回数据
$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);
已添加:
返回数组中的数据
<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
)
所以我认为json_encode是一个问题。也许
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
)
有36000多个产品记录
答案 0 :(得分:2)
这似乎是一个奇怪的编码问题。在while循环中添加了一行,以强制将编码转换为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);
答案 1 :(得分:0)
尝试将循环更改为以下内容:
foreach($res as $val) {
$rows[] = $val['product'];
}
此外,如果仍然无法使用。然后:
echo "<pre>";
print_($res);
die();
,请发布输出。
编辑:1
您不需要foreach循环。
您可以直接这样做:
$data = json_encode($res);
echo $data;