Epitheoritis 32
Epitheoritis 32

Reputation: 366

Binding result of a query with PHP variable

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

Answers (2)

user11436421
user11436421

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

ScaisEdge
ScaisEdge

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

Related Questions