Wally Fisher
Wally Fisher

Reputation: 39

Undefined index error message mysqli

I am new to mysqli but have some programming background. I have a database with three fields in mysql

db: userId (primary key, bigInt(2) Auto Increment, f_Name (varchar) and l_Name (varchar)

I have a code procedure that connects to the db and executes a query. Both work. When I try to view the results of the query, the f_name & l_name display but I get an error message undefined index on the userId field. I have tried some of the other solutions mentioned on this site without success. Any help would be appreciated.

<?php
    try {
    $sql = 'SELECT userID, f_Name, l_Name FROM users';
    if ($sql !== false) {
        echo 'done';
     } else {
        echo 'sql not working';
    }
    $result = $mysqli->query($sql);
    } catch (Exception $e) {
        $error = $e->get_message();
    }

    //view results of the query $sql
    while ($row = $result->fetch_assoc()) {
    echo $row["userId"];  //this is the line that generates the error
    echo $row['f_Name'];
    echo $row['l_Name'] . "<br>";
    varchar
    }
?>

Upvotes: 1

Views: 3964

Answers (1)

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

You need to change:

 echo $row["userId"];

to:

 echo $row["userID"];

You're creating an index with userID from the database, so when you call it in your associative array, you need to make sure the case is correct.

Upvotes: 2

Related Questions