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条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题