I have a little problem, I don't understand.
I have this code :
public function formAlreadySent($demande){
$data = odbc_exec($this->dbConnection,"SELECT COUNT(*) FROM [@JDC_QSTR] WHERE U_Demande = ".$demande);
$nombre = odbc_fetch_array($data);
print_r($nombre);
if($nombre[0] > 0)
return true;
else
return false;
}
The result of my print_r
is :
Array ( [0] => 0 )
But I got this error :
Notice: Undefined offset: 0 in C:\wamp\www\enquetesatisfaction\libs\model\QuestionsManager.class.php on line 89
Line 89 is :
if($nombre[0] > 0)
If someone know why this problem happen ?
Thanks in advance !
According to the PHP documentation, odbc_fetch_array
fetches a result row and returns it as an associative array, or returns false
if there are no more rows.
Being an associative array, the index of the first item (COUNT(*)
) should not be [0]
. To be honest, I don't know why it apparently is.
I would suggest this: first alias the count in your SQL, like
SELECT COUNT(*) AS rowcount ...
Then reference that alias after you fetch the result.
if($nombre['rowcount'] > 0) ...
as per the var_dump
output:
array (size=1) '0' => string '0' (length=1)
The reason for error
Notice: Undefined offset: 0 in
is that it is a string
and you are accessing as an integer
It can be stooped using if
with key but I would suggest to check using PHP funciton array_key_exists