Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2012
    Posts
    4

    Unanswered: mysql record: notice undefined index

    Hi everyone, I'm new here and I have some experience with php/mysql/databases, but I'm getting a notice that I never had before.

    Here is my code:
    $sql = "SELECT vehicles.vehicle_quantity FROM vehicles WHERE vehicle_id = 1";
    $result = mysql_query($sql, $connect) or die(mysql_error()); // Did we get a result?
    $record = mysql_fetch_array($result);
    $amount = htmlentities($record['amount']);
    echo "$amount";

    When I run the code in a webpage I get: Notice: Undefined index: amount in /home2/htmlprac/public_html/php/traffic_light/traffic_light_script.php on line 30

    line 30 is: $amount = htmlentities($record['amount']);

    When I run it in directly in phpmyadmin I get 33, which is what I was expecting to get.

    What I don't understand is why I'm getting this notice. I'm using the same code format with other databases and I'm not having a problem running the code in a webpage.

    If I run a sum on vehicle_quantity it works fine.

    my code:

    $sql = "SELECT SUM(vehicle_quantity) AS vehicle_total FROM vehicles";
    $result = mysql_query($sql, $connect) or die(mysql_error()); // Did we get a result?
    $record = mysql_fetch_array($result);
    $vehicle_total = htmlentities($record['vehicle_total']);

    I can't figure out why $vehicle_total = htmlentities($record['vehicle_total']); $record works with the select sum statement but not the previous select statement above.

    And again it works fine when I run it directly in phpmyadmin.
    Last edited by 2161; 09-23-12 at 09:43.

  2. #2
    Join Date
    Mar 2004
    Posts
    480
    you have a PHP question not a mysql question.

  3. #3
    Join Date
    Sep 2012
    Posts
    4
    Thanks for the advice, I'll move it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •