Have a issue with comma "," when I am fetching the data if the data for selected id is empty then also the coma(,) shows up what i want is if the data is null or empty the "," should not show up which am unable to do.
say I am selecting the data for
id 1 ,2 ,3
Where for ID 1
Num is 6789
and ID 2
has no Num and id 3 has 12345
$query="SELECT Num FROM euser WHERE UserId IN (SELECT ID FROM master WHERE aa='aa' And cc='cc')" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
$Num = array();
while($row=mysqli_fetch_array($data)){
$Num[] = $row['Num'];
}
$NumStr = implode(',', $Num);
echo $NumStr;
mysqli_close($mysqli);
Present output
6789,,12345
Expecting output
6789,12345
Use isset()
while ($row = mysqli_fetch_array($data)) {
if (isset($row['Num']) && $row['Num'] != "") {
$Num[] = $row['Num'];
}
}
$NumStr = implode(',', $Num);
Replace you code with below code.
Added a check for blank and NULL.
$query="SELECT Num FROM euser WHERE UserId IN (SELECT ID FROM master WHERE aa='aa' And cc='cc')" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
$Num = array();
while($row=mysqli_fetch_array($data)){
if($row['Num'] != '' && $row['Num'] != NULL){
$Num[] = $row['Num'];
}
}
$NumStr = implode(',', $Num);
echo $NumStr;
mysqli_close($mysqli);
$query="SELECT Num FROM euser WHERE UserId IN (SELECT ID FROM master WHERE aa='aa' And cc='cc')" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
$Num = array();
while($row=mysqli_fetch_array($data)){
$Num[] = $row['Num'];
}
$final_arr = array_filter($Num);
$NumStr = implode(',', $final_arr);
echo $NumStr;
mysqli_close($mysqli);
You just have to use "array_filter" function to remove empty/null values from the array.