I have a SQL query and an array where I put values to exclude. When I perform my query, I would like to test my variable.
If not empty ==> Display values
If empty ==> Display one time 0 result.
My SQL query gives in output these values:FGFR1e12, FGFR3e7, FGFR3e14
When I perform my php script, I manage to display values but it doesn't enter in the loop else when the variable is empty.
Does anyone know what I am doing wrong in my script ?
Here's my script:
<?php
require_once ('config.php');
$VariantNContrib = "SELECT DISTINCT Reference FROM mytable";
$PerformVariantNContrib = mysqli_query($conn, $VariantNContrib) or die(mysqli_error($conn));
// Values to exclude are stored in an array
$arrayNoContrib = array(
'FGFR1e12',
'FGFR3e7',
'FGFR3e14'
);
while ($rowVarNContrib = mysqli_fetch_assoc($PerformVariantNContrib)) {
if (!in_array($rowVarNContrib["Reference"], $arrayNoContrib)) {
$NContribList = '' . implode(',', $rowVarNContrib) . '; ';
if (!empty($NContribList)) {
echo '<br/>Variants:' . $NContribList . '<br/>';
}
else {
echo "0 results";
}
}
}
?>
var_dump()of$NContribListto see what it contains. That should tell you why it's not working. Alternatively, just replaceif(!empty($NContribList))withif(!count($rowVarNContrib))to check the number of elements in the array directly rather than looking at the string. Also, off topic, but I would urge you to tidy up your code so that the intentation is correct -- the messy layout of the code you've posted makes it really hard to read.$arrayNoContrib = array('FGFR1e12', 'FGFR3e7','FGFR3e14'); var_dump($NContribList)displaysnothing. When$arrayNoContrib = array('FGFR1e12'); var_dump($NContribList)displaysstring(7) "FGFR3e7" string(8) "FGFR3e14"