在php中清空$array[]的旧值
我正在尝试删除我在internet上搜索的数组的旧值。我刚刚找到了使用此方法的方法unset(),但我的情况似乎有所不同。下面是我的完整代码在php中清空$array[]的旧值,php,mysql,Php,Mysql,我正在尝试删除我在internet上搜索的数组的旧值。我刚刚找到了使用此方法的方法unset(),但我的情况似乎有所不同。下面是我的完整代码 <?php header('Content-type: application/json; charset=utf-8'); $link = mysqli_connect("localhost","root",""); mysqli_query($link,"SET NAMES UTF8"); $db= mysqli_select_db($li
<?php
header('Content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost","root","");
mysqli_query($link,"SET NAMES UTF8");
$db= mysqli_select_db($link,"Mirsa") or die("Cannot select Database.");
if (isset($_GET['Product_ID']) ){
$productId;
$Menu_ID =$_GET['Product_ID'];
$query = "SELECT
mirsaProductId,mirsaProductCode,mirsaProductDescription,
mirsaProductPcsInCartoon,mirsaProductWeight,mirsaProductVolume,
mirsaProductCodeBType,mirsaProductCodeCType,FK_mirsaCategoryId
from mirsaProduct WHERE FK_mirsaCategoryId='$Menu_ID'";
$result = mysqli_query($link,$query) or die(mysql_error($link));
$response["Products"] = array();
$products = array();
while ($row = mysqli_fetch_array($result)) {
$image[]=array()
$products["mirsaProductId"] = $row["mirsaProductId"];
$products["mirsaProductCode"] = $row["mirsaProductCode"];
$products["mirsaProductDescription"] = $row["mirsaProductDescription"];
$products["mirsaProductPcsInCartoon"] = $row["mirsaProductPcsInCartoon"];
$products["mirsaProductWeight"] = $row["mirsaProductWeight"];
$products["mirsaProductVolume"] = $row["mirsaProductVolume"];
$products["mirsaProductCodeBType"] = $row["mirsaProductCodeBType"];
$products["mirsaProductCodeCType"] = $row["mirsaProductCodeCType"];
$productId = $row["mirsaProductId"];
$query2 = "SELECT mirsaProductUrlImage FROM mirsaProduct,mirsaProductImages
WHERE FK_mirsaProductId = '$productId' GROUP BY mirsaProductUrlImage";
$result2=mysqli_query($link, $query2);
while ($row2 = mysqli_fetch_array($result2))
{
$image [] = $row2["mirsaProductUrlImage"];
$products["mirsaProductUrlImage"]=$image;
}
array_push($response["Products"], $products);
}
die(json_encode($response));
}
// echoing JSON response
else {
// no products found
$response["success"] = 0;
$response["message"] = "No Products";
die(json_encode($response));
}
?>
第二个JSON对象不包含这2个图像url,它接受$image[]的旧值,因此我需要重新初始化或清空他的值,以某种方式我需要你们的帮助伙计们,你们永远是我们的支持我要尝试一下。因为您从未真正将
$image
初始化为数组。你真的打算:
$image[] = array()
将是:
$image = array();
这将导致每次通过第一个while循环时,
$image
数组都会被重置。您的代码容易受到攻击。您应该使用或准备带有绑定参数的语句,如中所述。我知道这只是一个api,为我的iOS应用程序获取json值没有事务没有秘密信息…让我们关注如何解决我的问题,然后我将更新我的代码如果我理解正确的问题,您不需要$image数组。因此,取而代之的是->$image[]=$row2[“mirsapProductUrlImage”]$产品[“mirsapProductUrlImage”]=$image;使用此->$products[“mirsapProductUrlImage”]=$row2[“mirsapProductUrlImage”];不,伙计,我需要数组[],但每次他结束循环时,我都需要清空数组仍然得到相同的响应请大家帮忙
$image = array();