2 回答
TA贡献1780条经验 获得超3个赞
您在每次迭代中覆盖 while 循环内的所有三个变量,这就是为什么您只得到一个结果(这是循环内执行的最后一个数据集while。
您基本上可以采取 3 件事来解决您的问题。
1.使用数组存储循环得到的所有结果
while ($y=mysqli_fetch_array($sql)) {
$mrk[] = implode("", DekripCBC($y['nama_merk']));
$mdl[] = implode("", DekripCBC($y['nama_model']));
$wrn[] = implode("", DekripCBC($y['warna']));
}
2.使用预定义的数组来存储使用 循环获得的所有结果array_push。
/**There are 3 ways to define an empty array. You can use any of the following
1.$emptyArray = [];
2.$emptyArray = array();
3.$emptyArray = (array) null;
* */
$dataSet =[];
while ($y=mysqli_fetch_array($sql)) {
$data['mrk'] = implode("", DekripCBC($y['nama_merk']));
$data['mdl'] = implode("", DekripCBC($y['nama_model']));
$data['wrn'] = implode("", DekripCBC($y['warna']));
array_push($dataSet ,$data);
}
3.将值连接到变量中
while ($y=mysqli_fetch_array($sql)) {
// I have added a seprator(,) for all the variables so that there will be a , after every result and it wouldn't make you confused at the end
$mrk .= implode("", DekripCBC($y['nama_merk'])).',';
$mdl .= implode("", DekripCBC($y['nama_model'])).',';
$wrn .= implode("", DekripCBC($y['warna'])).',';
}
我强烈建议您使用方法 1 或方法 2。
我希望这能帮助您解决您的问题。
如果您想使用查询搜索多个 id:(因为我在评论中提到,我会将其添加到此处。)
前任:
//You have multiple ids that you get from the session.
$jns = [12,13,4,5];
//So now you can use a foreach loop to loop all the ids and use the query to get relevant data
foreach($jns as $id){
$sql = mysqli_query($kns, "Select stok.id_stok as id_stok, merk.nama_merk as nama_merk, model.nama_model as nama_model, stok.warna as warna FROM stok INNER JOIN model On stok.id_model=model.id_model INNER JOIN merk ON model.id_merk=merk.id_merk where id_stok = '$id' ") or die(mysqli_error($kns));
while ($y=mysqli_fetch_array($sql)) {
$mrk[] = implode("", DekripCBC($y['nama_merk']));
$mdl[] = implode("", DekripCBC($y['nama_model']));
$wrn[] = implode("", DekripCBC($y['warna']));
}
}
通过这种方法,您可以获取上面提到的 ids 的所有数据并将其分配给数组。
TA贡献1783条经验 获得超4个赞
导致此问题的原因是它添加到变量中,并且由于相同的变量名称,相同的变量每次都会被覆盖。您要么需要清空这些变量,要么将它们推送到其他地方并可以使用它。
为此,我们可以使用 php 函数 array_push 将它们推送到主数组中。
$master = array();
while ($y=mysqli_fetch_array($sql)) {
$data['mrk'] = implode("", DekripCBC($y['nama_merk']));
$data['mdl'] = implode("", DekripCBC($y['nama_model']));
$data['wrn'] = implode("", DekripCBC($y['warna']));
array_push($master,$data);
}
- 2 回答
- 0 关注
- 105 浏览
添加回答
举报