-2

I have simple inventory DB. When trying to access the products table and display the data in a table, i continually get "Unidentified index" and nothing i have found thus far can assist me. I am just starting PHP

conn.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "slfs_storesb";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_errno) {
    printf("Connection failed: %s\n" . $conn->connect_error);
} 

?>

index.php

<?php
            $sql = "select * from tbl_station";
            $result = mysqli_query($conn,$sql);
            while($row = mysqli_fetch_assoc($result)) {
                var_dump($row);
                // print $row[4];
                //echo '<table id="t01">';
                echo $row["Station_id"]." ".$row["Station_name"]."  ".        $row["Station_email"];
                // echo '</table>';

                    }

               // } else {
                   // echo "0 results";
               // }
            ?>

THis is the error

Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0010  246504  {main}( )   ..\index_1.php:0
Gros Islet Fire Station
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0010  246504  {main}( )   ..\index_1.php:0
GFL Fire Hall
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0010  246504  {main}( )   ..\index_1.php:0
HeadQuarters
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0010  246504  {main}( )   ..\index_1.php:0
Dennery class footer { This is footer } 
fivbag
  • 1
  • 3

1 Answers1

0

Instead of using SELECT * just spell out the (three) fields you want to access.
This way the query will fail if one of the fields is misspelled (or simply doesn't exist in that table).

<?php
$sql = '
    SELECT
        Station_id, Station_name, Station_email
    FROM
        tbl_station
';
$result = $conn->query($sql)
    or trigger_error('query failed: '.join(',', $conn->error_list));

while( $row=$result->fetch_assoc() ) {
    echo $row['Station_id'], ' ', $row['Station_name'],' ', $row["Station_email"], "<br />\r\n";
}
Community
  • 1
  • 1
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Solved!! And no the problem was not already solved, mine seem to be unique. I originally created the database and tables with navicat, for some reason this may have been the issue (dont ask). I dropped the tables one by one and created them using phpmyadmin; same code, same tables, same column names. Now it works fine. Thank you for those who tried assisting me in any way. – fivbag Jul 15 '15 at 01:25