0

我让自己纠结于使用 pdo 在表中显示 MySQL 数据的 pdo 语句。

我的语法是:

 $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;

然后我尝试获取输出

<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>

这会产生错误:调用非对象上的成员函数 bindParam()

任何援助将不胜感激。亲切的问候,

4

4 回答 4

2

这是你的脚本:

    $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;

将此更改为:

  $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;

然后尝试:

  <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>

您已bindparam在非对象上使用..USEprepare而不是query.

于 2012-12-12T12:45:46.293 回答
2

使用prepare方法不适query用于准备好的语句。

和:

  • 方法调用需要括号,否则对于 PHP,它将是一个属性:$result->execute();而不是$result->execute;
  • 你应该迭代 $result 而不是 $jobs:while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
于 2012-12-12T12:43:05.257 回答
2

这意味着您的查询没有解析,并且 $result 不是 PDO 语句对象。转储$db->e​​rrorInfo()以获取有关错误的更多详细信息。

它可能就像 ":end>" 在 ">" 之前需要一个空格一样简单——我知道我倾向于把这些东西隔开,也许是因为过去的痛苦:)

于 2012-12-12T12:41:11.520 回答
0

答案的获取部分有一个错字。

<? echo $row['icommenttype];?>应该<? echo $row['icommenttype'];?>

我试图对其进行编辑,但由于 SO 的最少 6 个字符的编辑政策而被拒绝。

于 2013-12-18T21:38:47.733 回答