tags:

views:

71

answers:

4

when accessing a set of information in a database i am then trying to print the names, however i get an undefined index error:

$val= mysql_query("select * from country"); 
while($row = mysql_fetch_array($val)){
    echo $row['country'];
}

i think it maybe to do with not check if isset correctly, is there an example related ot this that someone could give me so i can apply it to my own example?

thanks

+1  A: 

That means you have no column called country in the table.

You can use print_r($row); to see all the indexes that are in it.

If you're only going to look at named indexes you can use mysql_fetch_assoc() instead.

Greg
You beat me to it :(
Filip Ekberg
+1  A: 

You need to specify the field name not the table name in the $row[] statment

It looks like at the moment you are trying to output the table name. See below.

      $val= mysql_query("select * from TABLENAME"); 
        while($row = mysql_fetch_array($val)){
                echo $row['FIELDNAME'];
        }
Pino
+1  A: 

The problem is that you have no row named country.

Take this as an example:

$val= mysql_query("select id, name, continent, population from country"); 
while($row = mysql_fetch_array($val)) {
    echo $row['id'];
    echo $row['name'];
    echo $row['continent'];
    echo $row['population'];
}

The thing is that you need to print the available collums. For that you could also use print_r() that will output you the array, as said in the manual, in a human-readable style.

Frankie
A: 

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

  1. 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.
  2. Debug the returned Array using print_f, see if you can access the index by numeric value instead of key.
  3. 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.

Filip Ekberg