问题
I I am getting myself tangled with a pdo statement to display MySQL data in a table using pdo.
my syntax is:
$startdate=$_POST["start"];
$enddate=$_POST["end"];
$ttype=$_POST["ttype"];
$result = $db->query("SELECT cycletype,commenttype,adminstatus FROM v2loads where haulier= :haulier and :start < sarrive and :end> sarrive order by sarrive");
$result->bindParam(':haulier', $company, PDO::PARAM_STR);
$result->bindParam(':start', $startdate, PDO::PARAM_STR);
$result->bindParam(':end', $enddate, PDO::PARAM_STR);
$result->execute;
I then try t fetch the output with
<table>
<? while($row = $jobs->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td>
<? echo $row['cycletype'];?>
<td>
<td>
<? echo $row['icommenttype];?>
<td>
<td>
<? echo $row['adminstatus'];?>
<td>
</tr>
<? } ?>
</table>
This produces error: Call to a member function bindParam() on a non-object
Any assistance would be appreciated. Kind Regards,
回答1:
This is your script:
$startdate=$_POST["start"];
$enddate=$_POST["end"];
$ttype=$_POST["ttype"];
$result = $db->query("SELECT cycletype,commenttype,adminstatus FROM v2loads where haulier= :haulier and :start < sarrive and :end> sarrive order by sarrive");
$result->bindParam(':haulier', $company, PDO::PARAM_STR);
$result->bindParam(':start', $startdate, PDO::PARAM_STR);
$result->bindParam(':end', $enddate, PDO::PARAM_STR);
$result->execute;
Change This To:
$startdate=$_POST["start"];
$enddate=$_POST["end"];
$ttype=$_POST["ttype"];
$result = $db->prepare("SELECT cycletype,commenttype,adminstatus FROM v2loads where haulier= :haulier and :start < sarrive and :end> sarrive order by sarrive");
$result->bindParam(':haulier', $company, PDO::PARAM_STR);
$result->bindParam(':start', $startdate, PDO::PARAM_STR);
$result->bindParam(':end', $enddate, PDO::PARAM_STR);
$result->execute;
Then Try:
<table>
<? while($row = $result->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td>
<? echo $row['cycletype'];?>
<td>
<td>
<? echo $row['icommenttype];?>
<td>
<td>
<? echo $row['adminstatus'];?>
<td>
</tr>
<? } ?>
</table>
You have used bindparam on a non object..USE prepare instead of query.
回答2:
That means that your query didn't parse, and $result isn't a PDO statement object. Dump $db->errorInfo() for more details on the error.
It might be as simple as ":end>" needing a space before ">" -- I know that I tend to space such things out, maybe from past pain :)
回答3:
Use prepare method not query for a prepared statement.
And:
- method call needs parentheses else, for PHP, it will be a property:
$result->execute();instead of$result->execute; - you should iterate on $result not $jobs:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
回答4:
There is a typo in the fetch section of the answer.
<? echo $row['icommenttype];?> should be <? echo $row['icommenttype'];?>
I tried to edit it, but was denied because of SO's minimum 6 character edit policy.
来源:https://stackoverflow.com/questions/13839948/pdo-bind-variables-to-prepared-mysql-statement-and-fetch-using-while-loop-to-dis