I am trying to write the below function to display the contents of a MySQL table.
$q=$_GET["q"];
function risk_allocation($db)
{
$result = $db->query("select r.risks as risks,r.risktype as risktype,j.job as job from risks r LEFT OUTER JOIN `jobsrisks` j on r.risks = j.risk and j.job=:q");
$result ->bindParam(':q', $q, PDO::PARAM_INT);
return $result;
}
$allocationlist = risk_allocation($db);
I am then calling the finction with:
while($row = $allocationlist->fetch(PDO::FETCH_ASSOC))
{
echo $line['risks'];
echo $line['risktype'];
}
I am receiving error message:
Fatal error: Call to a member function bindParam() on a non-object in /home/she/public_html/versionfour/getrisksperjob.php on line 11
where line 11 is
$result ->bindParam(':q', $q, PDO::PARAM_INT);
I have the feeling this is a simple question resulting from me just being introduced to pdo, but any help appreciated as always.
UPDATE
as per proposed answers, my code is now as below to execute query and to include the variabe q. same error still occurs though. Thanks for help thus far, any ideas?
Fatal error: Call to a member function bindParam() on a non-object in /home/she/public_html/versionfour/getrisksperjob.php on line 11
function risk_allocation($db,$q)
{
$result = $db->query("select r.risks as risks,r.risktype as risktype,j.job as job from risks r LEFT OUTER JOIN `jobsrisks` j on r.risks = j.risk and j.job=:q");
$result ->bindParam(':q', $q, PDO::PARAM_INT);
$result->execute();
return $result;
}
$allocationlist = risk_allocation($db,$q);
1.You
forgot to execute query2.
$qisoutside function so you will never get value of that inside function.pass $q as second parameter of function