0

I get the following error Fatal error: Call to a member function execute() on a non-object in /home/wt/public_html/view-reports.php on line 237 .

I also see another problem. The variables generated doesn't echo the right corresponding data. For instance, $rep_type echo position. Could you please help me on this. Thank you! Test site url is here. ethioserver.com/~wt/view-reports.php?rep_id=144 The problem is not visible in wampserver. The php version in both servers is 5.4.

The main code is below.

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

The problem area.

  $_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();

The line with error. is

 $stmt->execute();

Thanks!

latonz
  • 1,601
  • 10
  • 21
wondim
  • 697
  • 15
  • 29
  • I'm not getting an error. What output do you get if you put `print_r($con);` immediately after `require ('includes/db.php');`? – bloodyKnuckles Jun 06 '14 at 17:17
  • I got mysqli Object ( ) when I added print_r($con). I still see the other errors though. – wondim Jun 06 '14 at 21:28
  • `print_r`'s don't fix things, they are tools to help you figure out what needs fixing. So, $con is a mysqli object. Next put `print_r($stmt);` immediately after `$stmt = $con->prepare($sql);` and post what output you get. – bloodyKnuckles Jun 07 '14 at 15:54
  • I now get. Nothing is displayed after I added what you said. – wondim Jun 08 '14 at 10:18
  • Then the problem appears to be with `$stmt = $con->prepare($sql);`, because when I put `print_r($stmt);` immediately after it I'm getting `mysqli_stmt Object ( [affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 0 [field_count] => 1 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 1 )`. – bloodyKnuckles Jun 08 '14 at 17:19
  • Yes, that is right. I did the same thing on the local site and I got the output you posted above. Is it because some php extension is not enabled that the prepare statement is not working? Which extension could that be? – wondim Jun 09 '14 at 09:57
  • If the `print_r($con)` gives you an mysqli object, then I'm sure you have the php extensions you need, and the problem is in either the `mysqli_select_db`, but more likely the `$con->prepare`. – bloodyKnuckles Jun 09 '14 at 13:35

1 Answers1

0

I expect some error checking ought to shed light on the problem. Add or die(mysqli_error($con)) after the mysqli_select_db and $con->prepare functions:

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();

Regarding the mismatched variables, apparently the order of table columns is not corresponding with the order of the variable assignments. I'm guessing at your table names but I expect this gives you the idea. Change or $sql assignment to:

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

Alternatively you could reorder the variable assignments to match the order of the columns in the database table Reports, however the way I showed you above will not break if someone where to add or reorder table columns in the future.

On a side note, what is the purpose of this line at the top:

$_GET['rep_id']>0;

I don't see an assignment, or conditional evaluation. Looks like it's doing nothing.

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
  • I made the changes as what you said but I am now getting this error. Fatal error: Allowed memory size of 4294967296 bytes exhausted (tried to allocate 4294967296 bytes) in /home/wt/public_html/view-reports.php on line 237 . That line is where there is ' bind_result ' is located. I tried to increase the memory up to 1.6GB but it still shows the same error. Thank you very much for the help! – wondim Jun 10 '14 at 08:04
  • I hope this helps: http://stackoverflow.com/questions/18121619/allowed-memory-size-of-134217728-bytes-exhausted-tried-to-allocate-4294967296-b – bloodyKnuckles Jun 10 '14 at 14:11
  • Everything is now solved!!! The mismatch was because of my mistake. The link you shared helped me. I made text size to medium text on mysql and no error. Thanks so much for the help bloodyKnuckles. – wondim Jun 10 '14 at 18:07