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
?– Woss
Error indicates that the connection is empty or picked up the wrong variable.
– rray
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);".
– GABRIEL DO NASCIMENTO SOUZA