未定义的索引,当使用mysqli_fetch_assoc时,SELECT *

时间:2015-07-14 01:01:03

标签: php html mysqli

我有简单的库存数据库。当试图访问产品表并在表中显示数据时,我不断得到“未识别的索引”,到目前为止我找不到任何东西可以帮助我。我刚刚开始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";
               // }
            ?>

这是错误

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 } 

1 个答案:

答案 0 :(得分:0)

而不是using SELECT *只拼出您想要访问的(三个)字段 这样,如果其中一个字段拼写错误(或者根本不存在于该表中),查询将失败。

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

/*GET Hello World page. */
router.get('/helloworld', function(req, res){
    var first;
    yelp.search({location: "tempe", term: "sandwiches", category_filter: "food"}, function(error, data) {
        var test = JSON.stringify(data); //Changes yelps response to JSON Objects with double quotes
        test = JSON.parse(test); //JSON.parse can now parse correctly after stringify is used.
     //     for(var i = 0; i < 9; i++){
        //    console.log(test['businesses'][i]['name']);
        //  // console.log(test['businesses'][i]);
        // }
        console.log(test['businesses'][0]['name']);
        first = test['businesses'][0]['name'];

    });
    res.render('helloworld', {
            title: 'Hello, World',
            name: first
    });
});

module.exports = router;