ProEvilz
ProEvilz

Reputation: 5455

Why is my price column classed as undefined?

I'm quite new to MySql/php and I'm doing some testing/experimenting and what not and I've come across a little issue I'm not entirely sure of. I have two columns, Price and Username. I have at least 50 entries too.

<?php
if (isset($_POST['username']) {$username = $_POST['username'];} else {exit;}

$result = mysql_query("
      SELECT SUM(price), username 
      FROM paid_donations 
      WHERE username 
      LIKE '%$username%'");

$data = mysql_fetch_assoc($result);
?>

    <?php // inside html elements
    echo($data['price']) . "<hr> <br>";
    echo($data['username']) . "<hr> <br>";
    ?>

If I do not use SUM(price) and use only price it will work fine, otherwise it will throw out a notice :

Notice: Undefined index: price in localhost/test

and then the SUM will not show, no price at all will show, however, if I run that exact same query via PHPMyAdmin it will work as intended. An explanation as to why this is so, would be great.

Upvotes: 1

Views: 52

Answers (1)

ProEvilz
ProEvilz

Reputation: 5455

Problem was with not updating my code from:

 echo($data['price']) . "<hr> <br>";

to:

 echo($data['SUM(price)']) . "<hr> <br>";

I didn't think that would matter, so I overlooked it.

Upvotes: 2

Related Questions