0

Why is this code doing nothing? He isn't echoing the result out.. The goal of this script is to echo the average of a column (on 2 decimals).

<?php
if (isset($_GET["age"]));
$age = ($_GET["age"]);
include($_SERVER["DOCUMENT_ROOT"] . "/3/includes/config.php");

$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT AVG(price) FROM data WHERE age= '$age'") or die("Error: " . mysqli_error($con));

while($row = mysqli_fetch_array($result));
echo $row['price'];

die();

?>
Julian
  • 361
  • 1
  • 4
  • 18
  • did u try using curly braces "{}" with your while loop? – kerv Dec 08 '16 at 13:34
  • 1
    As per the manual http://php.net/manual/en/language.basic-syntax.instruction-separation.php *"As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon **terminating** the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present."* - PHP would not have thrown you an error about it, since the semi-colon is considered as a **valid** character. – Funk Forty Niner Dec 08 '16 at 13:36

3 Answers3

4

You have effectively put your echo statement after the loop:

while($row = mysqli_fetch_array($result));
                                         ^ this is the problem
echo $row['price'];

... is the same as:

while($row = mysqli_fetch_array($result)) {}
echo $row['price'];

And after the loop $row will be false so nothing will be echoed out.

You want:

while($row = mysqli_fetch_array($result))
    echo $row['price'];

or (better as it will avoid these kinds of mistakes...):

while($row = mysqli_fetch_array($result)) {
    echo $row['price'];
}

And you should be using a prepared statement to avoid the sql injection problem you have now.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 1
    You can [borrow this](http://stackoverflow.com/questions/41040692/php-script-mysqli-doesnt-echos-something#comment69288834_41040692) if you like ;-) – Funk Forty Niner Dec 08 '16 at 13:37
  • Thanks for your explanation. About prepared statements, I have heard a lot about it and did some research but I don't know how to implement it in my code. – Julian Dec 08 '16 at 13:40
  • @Fred-ii- I'm actually not sure, it seems that is about a block as `` and not a block as `{ ... }` – jeroen Dec 08 '16 at 13:40
  • And what about the 2 decimals? Someone said to me that I need to replace AVG(price) by ROUND(AVG(price AS FLOAT), 2) , but if I do that I get the following: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS FLOAT), 2) FROM data WHERE age= '15'' at line 1 – Julian Dec 08 '16 at 13:43
  • @WalkingForNepal You should probably start with the php manual or some examples here on SO. I prefer PDO myself and that has a slightly different syntax. – jeroen Dec 08 '16 at 13:44
  • @WalkingForNepal You can format it either in mysql or in php. What is the problem exactly with the result you are getting now? – jeroen Dec 08 '16 at 13:46
  • Right now I am getting: 7.500000 but it is an amount of money, so I only need 2 decimals. – Julian Dec 08 '16 at 13:48
  • @WalkingForNepal Personally I'd do presentation stuff like that in the view or in your case the php: `echo number_format($row['price'], 2);` – jeroen Dec 08 '16 at 13:54
  • 1
    @jeroen Great it worked! Thank you very much :-) – Julian Dec 08 '16 at 13:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130127/discussion-between-julian-and-jeroen). – Julian Dec 08 '16 at 14:36
1

Check here your while loop it is wrong

while($row = mysqli_fetch_array($result));
echo $row['price'];

it should be

while($row = mysqli_fetch_array($result)){
    echo $row['price'];
}
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
1

Check your first conditional statement and your while loop. I have refactored your code and included comments explaining the fix. Hope this helps you!

<?php
//Add braces to your conditional here encapsulating the block of code you want to run if the condion is true. Your code is dependent on $_GET['age'], so you don't want to run it if you don't have that data.
if (isset($_GET["age"])) {
    $age = ($_GET["age"]);

    include($_SERVER["DOCUMENT_ROOT"] . "/3/includes/config.php");

    $con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT AVG(price) FROM data WHERE age= '$age'") or die("Error: " . mysqli_error($con));

//You also need to make sure that you are encapsulating the proper code in your while loop, as well.

   while($row = mysqli_fetch_array($result)) {
       echo $row['price'];
   } //close while loop

   die();
} //close if statement ?>

As @jeroen said above, you should really be using prepared statements and I also recommend using the object oriented mysqli vs. the procedural. http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Full Stack Alien
  • 11,244
  • 1
  • 24
  • 37