shackwm60 Posted January 14, 2015 Share Posted January 14, 2015 Please explain the philosophy here.. if this is ok and generates no errors or Notices $result = mysqli_query($connection, $query); if (!$result) { die("Database Query Failed: " . mysql_error()); } else { } while ($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>Address</td>"; echo "<td>"; echo $row['streetaddress']; echo "</td></tr>"; } Why does this code which is seemingly the same - aside from the logic test - generate an Undefined Index Notice? if (!empty($row['child1name'])) { echo "<tr>"; echo "<td colspan=\"2\">"; echo $row['child1name'] . " (" . $row['child1age'] . ")"; } and is the correct answer to decalre all of these variables in advance? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/293925-undefined-index/ Share on other sites More sharing options...
CroNiX Posted January 14, 2015 Share Posted January 14, 2015 (edited) What is undefined? Perhaps 'child1age' doesn't exist in your $row. We know 'child1name' exists because you test for it, so 'child1age' is the only other thing there that could be causing an issue. Hard do know since you chopped off the code showing the actual query, and what you selected. Edited January 14, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293925-undefined-index/#findComment-1502955 Share on other sites More sharing options...
shackwm60 Posted January 14, 2015 Author Share Posted January 14, 2015 oh sorry.. well its a select * so everything is there. The result echos everything i just get a NOTICE undefined index. Quote Link to comment https://forums.phpfreaks.com/topic/293925-undefined-index/#findComment-1502960 Share on other sites More sharing options...
CroNiX Posted January 14, 2015 Share Posted January 14, 2015 Usually it doesn't just say "undefined index". It says which index is undefined and which line it is occurring on. So exactly what does the notice say? But again, my guess is that child1age isn't a field in the table you are querying, or isn't present in the result for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/293925-undefined-index/#findComment-1502963 Share on other sites More sharing options...
Solution shackwm60 Posted January 14, 2015 Author Solution Share Posted January 14, 2015 oh duh.. i just figured it out.. im so stupid. i just moved this new code over from a test server but forgot to move the new db structure over.. so those fields dont exist yet sleep time... Quote Link to comment https://forums.phpfreaks.com/topic/293925-undefined-index/#findComment-1502964 Share on other sites More sharing options...
Jacques1 Posted January 15, 2015 Share Posted January 15, 2015 If you properly selected the specific columns instead of just doing a “SELECT *”, you wouldn't have those problems in the first place. The “*” is very poor style and commonly leads to bugs like this. Quote Link to comment https://forums.phpfreaks.com/topic/293925-undefined-index/#findComment-1503007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.