【问题标题】:Prepared statement execute fatal error and variables and database table miss match准备好的语句执行致命错误并且变量和数据库表未匹配
【发布时间】:2014-07-27 23:22:45
【问题描述】:

我收到以下错误 Fatal error: Call to a member function execute() on a non-object in /home/wt/public_html/view-reports.php 第 237 行。

我还看到了另一个问题。生成的变量不会回显正确的相应数据。例如,$rep_type 回显位置。你能帮我解决这个问题吗?谢谢!测试站点网址在这里。 ethioserver.com/~wt/view-reports.php?rep_id=144 该问题在 wampserver 中不可见。两台服务器的php版本都是5.4。

主要代码如下。

<?php
//generate page
     $_GET['rep_id']>0;
  if ($_GET['rep_id']!=0){
  require ('includes/db.php');
  mysqli_select_db($con, $db_name);
  $sql= 'SELECT * FROM Reports';
  $stmt = $con->prepare($sql);

  $stmt->execute();

  $stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $rep_type, $department, $position, $report, $rep_to);
  $stmt->fetch();
//allow users to edit

        if($fname . ' '. $lname!=$rep_by){
            echo '<div class="links"></div>';
        } 
        else {
            echo '<div class="links">';
            echo '<a href="edit-this-report.php?rep_id=' . $rep_id;
            echo 'target="_blank" img src="images/pdf.png" alt="edit report"  target="_blank" ><img src="images/edit.png"> </a>';
            echo '</div>';
        } 
echo '<div class="links"><a href="pdf/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.pdf"  
        target="_blank" img src="images/pdf.png" alt="download report"><img src="images/pdf.png"> </a></div>'; 

  //start html creation
ob_start();
 echo "<h1>$rep_by ($rep_date)</h1>";
 echo '<div class="infobar"><strong>Report Type: </strong>'. $position . '</div>' ;
 echo '<div class="infobar"><strong>Department: </strong>'. $rep_type  . '</div>';
 echo '<div class="infobar"><strong>Position: </strong>'.  $department . '</div>';
 echo $report; 
 file_put_contents(('scripts/dompdf/html/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.html'), ob_get_contents()); 


if  ($rep_ledit_date>0) {echo '<div class="infobar">' . 'Last Edited: ' . $rep_ledit_date . '</div>';


} else  {echo "";
//end html creation
ob_end_flush();}
//generate pdf using dompdf
require_once "scripts/dompdf/dompdf_config.inc.php";
$file='scripts/dompdf/html/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))) . '.html';
$dompdf = new DOMPDF();
$dompdf->load_html_file($file);
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For the header
$header = $canvas->open_object();
    $font = Font_Metrics::get_font("helvetica", "bold");
    $date = date("Y-m-d H:i:s");
    $canvas->page_text(500, 20, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0, 0, 0));
    $canvas->page_text(30, 20,  "Crystal Reporting System", $font, 8, array(0, 0, 0));
    $canvas->page_text(300, 20,  $date, $font, 8, array(0, 0, 0));
    $canvas->close_object();
    $canvas->add_object($header, "all");

//For Footer
$footer = $canvas->open_object();
    $font = Font_Metrics::get_font("helvetica", "bold");
    $canvas->page_text(30,750, "$rep_by  $position", $font, 8, array(0, 0, 0));
    $canvas->page_text(440,750,  "crystalreportingsystem.com", $font, 8, array(0, 0, 0));
    $canvas->close_object();
    $canvas->add_object($footer, "all");
$output = $dompdf->output();


file_put_contents('pdf/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.pdf', $output);
}
else {

 ?>
<h1>View Latest Reports</h1>
<p> You can now view all reports by clicking on the links on the side menu. </p>

<?php
}
?>

问题区域。

  $_GET['rep_id']>0;
  if ($_GET['rep_id']!=0){
  require ('includes/db.php');
  mysqli_select_db($con, $db_name);
  $sql= 'SELECT * FROM Reports';
  $stmt = $con->prepare($sql);

  $stmt->execute();

  $stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $rep_type, $department, $position, $report, $rep_to);
  $stmt->fetch();

有错误的行。是

 $stmt->execute();

谢谢!

【问题讨论】:

  • 我没有收到错误消息。如果你把print_r($con); 紧跟在require ('includes/db.php'); 之后,你会得到什么输出?
  • 我在添加 print_r($con) 时得到了 mysqli Object ()。不过,我仍然看到其他错误。
  • print_r 不会解决问题,它们是帮助您找出需要解决的问题的工具。所以,$con 是一个 mysqli 对象。接下来将print_r($stmt); 紧跟在$stmt = $con-&gt;prepare($sql); 之后,然后发布你得到的输出。
  • 我现在明白了。添加你说的内容后什么都没有显示。
  • 那么问题似乎出在$stmt = $con-&gt;prepare($sql); 上,因为当我把print_r($stmt); 紧跟在它后面时,我得到了mysqli_stmt Object ( [affected_rows] =&gt; 0 [insert_id] =&gt; 0 [num_rows] =&gt; 0 [param_count] =&gt; 0 [field_count] =&gt; 1 [errno] =&gt; 0 [error] =&gt; [error_list] =&gt; Array ( ) [sqlstate] =&gt; 00000 [id] =&gt; 1 )

标签: binding prepared-statement


【解决方案1】:

我希望一些错误检查应该可以解决这个问题。在mysqli_select_db$con-&gt;prepare 函数之后添加or die(mysqli_error($con))

require ('includes/db.php');
mysqli_select_db($con, $db_name) or die(mysqli_error($con));
$sql= 'SELECT * FROM Reports';
$stmt = $con->prepare($sql) or die(mysqli_error($con));

$stmt->execute();

关于不匹配的变量,显然表列的顺序与变量赋值的顺序不对应。我猜你的表名,但我希望这能给你这个想法。更改或$sql 分配为:

$sql= 'SELECT rep_id, rep_date, rep_ledit_date, rep_by, rep_type, department, position, report, rep_to FROM Reports';

或者,您可以重新排序变量分配以匹配数据库表 Reports 中列的顺序,但是如果将来有人在哪里添加或重新排序表列,我上面向您展示的方式不会中断。

顺便说一句,上面这行的目的是什么:

$_GET['rep_id']>0;

我没有看到作业或条件评估。好像什么都没做。

【讨论】:

  • 我按照您所说的进行了更改,但现在出现此错误。致命错误:第 237 行 /home/wt/public_html/view-reports.php 中允许的内存大小为 4294967296 字节已用尽(尝试分配 4294967296 字节)。该行是“ bind_result ”所在的位置。我尝试将内存增加到 1.6GB,但仍然显示相同的错误。非常感谢您的帮助!
  • 希望对您有所帮助:stackoverflow.com/questions/18121619/…
  • 现在一切都解决了!!!不匹配是因为我的错误。你分享的链接帮助了我。我在 mysql 上将文本大小设置为中等文本,没有错误。非常感谢血腥Knuckles 的帮助。
猜你喜欢
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多