First of all, for debugging purpose you can always use print_f.
I would modify the above to see if it even returns something and it might look something like this:
$val= mysql_query("select * from country");
while($row = mysql_fetch_array($val)){
print_f($row);
}
Now, undefined index usualy means that it cannot find the key / index you are looking for, in this case, it cannot find 'country' in your array ( $row
).
Step by step debugging
- Check the SQL - run the SQL in phpMyAdmin, Query Browser or any other SQL Command Line tool available to see that it really returns the result you want.
- Debug the returned Array using print_f, see if you can access the index by numeric value instead of key.
- If all of the above failed, your problem lies elsewhere, then the problem is not in the code you provided.
You want examples on isset, see the php manual for that.
Example of isset - taken from php.net
<?php
$var = '';
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
// In the next examples we'll use var_dump to output
// the return value of isset().
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE
unset ($a);
var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE
$foo = NULL;
var_dump(isset($foo)); // FALSE
?>
Side note
Never use select * from...
it's bad practice and results in errors like this, if you would specify the columns in your SQL, there would be less room for error.