douzhoulei8959 2014-09-22 16:51
浏览 66
已采纳

使用PHP查询MySQL数据库时,如何解决此未定义的索引错误?

I am learning PHP through the New Boston Youtube video tutorials.

When I run the query in the PHP script (script is below error message) on my localhost, I get an error message that is repeated twice. Please error message see below.

I want to be able to run this query within the PHP script and return the information I queried for.

Error message when I run index.php:

Notice: Undefined index: calories in /Applications/XAMPP/xamppfiles/htdocs/Database_To_Server/index.php on line 10

Notice: Undefined index: calories in /Applications/XAMPP/xamppfiles/htdocs/Database_To_Server/index.php on line 10

Code:

index.php

<?php
require 'connect.inc.php';

$query = "SELECT 'food' 'calories' FROM `food` ORDER BY 'id'";

if ($query_run = mysql_query($query)) {

    while ($query_row = mysql_fetch_assoc($query_run)) {
        $food = $query_row['food'];
        $calories = $query_row['calories'];
    }

} else {
    echo mysql_error();
}

?>

connect.inc.php

<?php
$conn_error = "Could not connect."; 

$mysql_host = 'localhost';
$mysql_user =  'root';
$mysql_pass = '';

$mysql_db = 'a_database';

if (!@mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !@mysql_select_db($mysql_db)) {
    die($conn_error);
} 
?>
  • 写回答

5条回答 默认 最新

  • douhuo0884 2014-09-22 16:53
    关注

    You're using the wrong identifiers for (and a missing comma between column names)

    $query = "SELECT 'food' 'calories' FROM `food` ORDER BY 'id'";
    

    do / and, remove the ' from about id

    $query = "SELECT `food`, `calories` FROM `food` ORDER BY id";
    
    • IF food isn't part of your columns (which seems to be the name of your table)

    do

    $query = "SELECT `calories` FROM `food` ORDER BY id";
    
    • just an insight.

    Footnotes:

    Your present code is open to SQL injection.
    Use prepared statements, or PDO with prepared statements.


    Edit

    To fix your present query, do:

    require 'connect.inc.php';
    
    $query = "SELECT `food`, `calories` FROM `food` ORDER BY `id`"; 
    
    $result = mysql_query($query) or die(mysql_error());
    
    while($query_row = mysql_fetch_assoc($result)){
        echo $query_row['food']. " - ". $query_row['calories'];
        echo "<br />";
    }
    

    As a learning curve, you should use mysql_error() to your advantage instead of just showing Could not connect., should there be a DB connection problem, therefore will not show you what the real error is.

    For example:

    <?php
    mysql_connect("localhost", "root", "") or die(mysql_error());
    echo "Connected to MySQL<br />";
    mysql_select_db("a_database") or die(mysql_error());
    echo "Connected to Database";
    ?>
    

    or from the manual - mysql_error()

    <?php
    $link = mysql_connect("localhost", "mysql_user", "mysql_password");
    
    mysql_select_db("nonexistentdb", $link);
    echo mysql_errno($link) . ": " . mysql_error($link). "
    ";
    
    mysql_select_db("kossu", $link);
    mysql_query("SELECT * FROM nonexistenttable", $link);
    echo mysql_errno($link) . ": " . mysql_error($link) . "
    ";
    ?>
    

    The above example will output something similar to:

    1049: Unknown database 'nonexistentdb'
    1146: Table 'kossu.nonexistenttable' doesn't exist

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改