2
I have an OOP code in PHP, when I try to connect with the BD appears the following error:
Notice: Undefined variable: Operator in C: xampp htdocs cadastroDelunos app.ado Tcriteria.class.php on line 16
Notice: Undefined variable: Conn in C: xampp htdocs cadastroDelunos app.ado Tconnection.class.php on line 46
Fatal error: Call to a Member Function setAttribute() on a non-object in C: xampp htdocs cadastroDelunos app.ado Tconnection.class.php on line 46
conection.ini
host = ['127.0.0.1']
name = ['sistema_escolar']
user = ['root']
pass = ['']
type = ['mysql']
Connection.php
<?php
function __autoload($classe)
{
if(file_exists("../app.ado/{$classe}.class.php"))
{
include_once "../app.ado/{$classe}.class.php";
}
}
$sql = new TSqlSelect;
$sql->setEntity('sistema_escolar');
$sql->addColumn('id');
$sql->addColumn('nome');
$criteria = new TCriteria;
$criteria->add(new TFilter('id','=','1'));
$sql->setCriteria($criteria);
try
{
$conn = TConnection::open('connection');
$result = $conn->query($sql->getInstruction());
if($result)
{
$row = $result->fetch(PDO::FETCH_ASSOC);
echo $row['id'].'-'.$row['nome']."\n";
}
$conn = null;
}
catch(PDOException $e)
{
print "Erro: ".$e->getMessage()."<br/>";
}
?>
Tcriteria.class.php
<?php
class TCriteria extends TExpression
{
private $expressions;
private $operators;
private $properties;
public function add(TExpression $expression, $operator = self::AND_OPERATOR)
{
if(empty($this->expressions))
{
unset($operator);
}
$this->expressions[] = $expression;
$this->operators[] = $operator;
}
public function dump()
{
if(is_array($this->expressions))
{
foreach($this->expressions as $i => $expression)
{
$operator = $this->operators[$i];
$result .= $operator . $expression->dump() . ' ';
}
$result = trim($result);
return "({$result})";
}
}
public function setProperty($property, $value)
{
$this->properties[$property] = $value;
}
public function getProperty($property)
{
return $this->properties[$property];
}
}
?>
Tconnection.class.php
<?php
final class TConnection
{
private function __construct()
{
}
public static function open($name)
{
if(file_exists("../app.config/{$name}.ini"))
{
$db = parse_ini_file("../app.config/{$name}.ini");
}
else
{
throw new Exception("Arquivo '$name' não encontrado");
}
$user = $db['user'];
$pass = $db['pass'];
$name = $db['name'];
$host = $db['host'];
$type = $db['type'];
switch($type)
{
case 'pgsql':
$conn = new PDO("pgsql:dbname={$name};user={$user};password={$pass};host={$host}");
break;
case 'mysql':
$conn = new PDO("mysql:host=".$host.";dbname={$name}",$user,$pass);
break;
case 'ibase':
$conn = new PDO("firebird:dbname={$name}",$user,$pass);
break;
case 'mssql':
$conn = new PDO("mssql:host={$host},1433;dbname={$name}",$user,$pass);
break;
}
$conn->setAttribute(PDO::ATTR_MODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
}
?>
A
var_dump
of the variable$db
in the methodTConnection::open
returns what? Your fileini
looks badly formatted (I don’t know if PHP accepts in the way you wrote).– Vinícius Gobbo A. de Oliveira
Both Connection.ini and php.ini are configured
– Renoir
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero