Problems with PHP function - PDO

Asked

Viewed 48 times

0

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\trofeugs2017\model\Model.php on line 81

How to correct the error?

Code:

<?php
class Model {

 protected $name_table;
 protected $fields;
 protected $id_row;
 public $con;


 public function __construct($name_table){
  $this->name_table = $name_table;
  $this->con = new PDO("mysql:host=localhost;dbname=trofeu2017", "root", ""); 
 }      
 function implodeFields($fields){
  return implode(', ', $fields);
 }
 function implodeKeys($fields){
  return implode(', ', array_keys($fields));
 }
 function implodeValues($fields){
  return "'" . implode("', '", array_values($fields)) . "'";
 }      
 public function updateTable(array $fields, $where){         
  if($where){
   $where = "WHERE {$where}";
  } else {
   $where = null;
  }
  foreach($fields as $key => $values){
   $campos[] = "`{$key}` = '{$values}'";
  }
  $fields = $this->implodeFields($campos);
  $sql = "UPDATE {$this->name_table} SET {$fields} {$where}";
  $update = $this->con->prepare($sql);
  if($update->execute()){
   return $update;
  } else {
   return false;
  }
 }
}
  • Your error is going on line 81, but your code only has 41 lines. That’s all the code in the file Model.php?

  • Error indicates that the connection is empty or picked up the wrong variable.

  • Personal thanks for the answer, but I ended up discovering the error, but responding vcs, the lines q omitted was from Insert and select, and the error would be in the line " $update = $this->con->prepare($sql);".

1 answer

0


Apparently, you are passing an empty or wrong variable in the command prepare

Use the find command of your IDE and look for prepare and check past variables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.