It seems this problem has just recently come up for me (I don’t remember having this issue in a similar scenario before). With the following code, php loops through and displays a form with values from a MySQL table. If any of the values contain an empty string I get the following notice:
Notice: Undefined variable: row in /home/public_html/index.php on line XXX
It ONLY happens when a string is empty.
This is the form code:
<?php
for ($i=1; $i<5; $i++) {
$val = $env->getVal("plan_title_$i,plan_desc_$i,plan_price_$i,plan_link_$i");
}
?>
And the portion of getVal code pulling the data:
while ($tmp = $res->fetch_object()->$col) {
$row[$keys[$i]] = trim((string)$tmp);
$i++;
}
Now I’ve thought of giving the column a default value of a space since that seems to solve the notice problem. However that’s not possible since it’s a BLOB field.
Does anyone know how I can prevent $row from being undefined if the value pulled is an empty string? This only happens on empty strings.
We found in the chat this to be the solution:
this will allow to enter the loop even when values in db are empty (empty strings). The while did not entered on empty values and this caused the return statement to show an undefined variable notice.