pdo bind variables to prepared mysql statement and fetch using while loop to display in table

ぃ、小莉子 提交于 2020-01-06 08:25:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!