Reputation: 366
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
Upvotes: 2
Views: 221
Reputation:
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
Upvotes: 1
Reputation: 133400
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>";
Upvotes: 1