2

I have this simple query that finds the average salary of a table named permanent_employee:

SELECT  AVG(E.salary) 
FROM employee as E,permanent_employee as P
WHERE E.empID=P.empID

What i want to to is bind this to a PHP variable.If this returned the salary i would bind it like this echo "<td>{salary}</td>"; and everything would work OK.However this echo "<td>{$AVG(E.salary)}</td>"; gives me errors.How could i make this query return a variable that can be later coverted to PHP form?

UPDATE: solution was to use AVG(E.salary) AS sth

3
  • 2
    Have you tried SELECT AVG(E.salary) as salary? Commented Jun 4, 2019 at 19:14
  • Well, you need to actually write some PHP code that executes the query and fetches the result to start with. Do you have any of that? Or just the query itself? Commented Jun 4, 2019 at 19:16
  • gives me errors? What error(s) do you get? Commented Jun 4, 2019 at 19:26

2 Answers 2

1

You need a proper alias

SELECT  AVG(E.salary)  my_avg 
FROM employee as E,permanent_employee as P
WHERE E.empID=P.empID


echo "<td>{$my_avg}</td>";
Sign up to request clarification or add additional context in comments.

Comments

1

There are multiple ways you can extract this to a php variable

$result = mysqli_query($con,"SELECT  AVG(E.salary)  my_avg 
FROM employee as E,permanent_employee as P
WHERE E.empID=P.empID");
$row = mysqli_fetch_array($result));
$avg = $row['my_avg'];
echo "<td>".$avg."</td>";

You can also use PDO, see Get results from from MySQL using PDO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.