I'm trying to make a rowCount using a class I myself developed for the connection and some functions involving bd.
I have the following PHP class:
class Banco {
private $debug;
private static $pdo;
function __construct($debug = true, $banco = "BANCO", $usuario = "USUARIO", $senha = "SENHA") {
$this->debug = $debug;
try {
$this->pdo = new PDO("mysql:host=HOST;dbname=".$banco, $usuario, $senha);
if($debug) {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
$this->pdo->query("SET NAMES 'utf8'");
$this->pdo->query("SET character_set_connection=utf8");
$this->pdo->query("SET character_set_client=utf8");
$this->pdo->query("SET character_set_results=utf8");
} catch (PDOException $e) {
if($this->debug) {
echo "Ocorreu um erro de conexão: " .$e->getMessage();
}
}
}
public function consultar($tabela, $colunas, $condicao, $agrupamento, $ordenacao, $limite) {
try {
if(is_array($colunas)) {
$colunas = implode(", ", $colunas);
}
$sql = "SELECT " .$colunas. " FROM " .$tabela;
if($condicao != false) {
$sql .= " WHERE " .$condicao;
}
if($agrupamento != false) {
$sql .= " GROUP BY " .$agrupamento;
}
if($ordenacao != false) {
$sql .= " ORDER BY " .$ordenacao;
}
if($limite != false) {
$sql .= " LIMIT " .$limite;
}
return $this->pdo->query($sql);
} catch (PDOException $e) {
echo "Ocorreu um erro: " .$e->getMessage();
}
}
public function contaLinha($sql) {
try {
$var = $this->pdo->query($sql);
return $this->pdo->rowCount($var);
} catch (PDOException $e) {
echo "Ocorreu um erro: " .$e->getMessage();
}
}
}
and another file, also in PHP, that needs this rowCount:
$bd = new Banco();
$colunas = array("nome", "email", "teste", "codigo", "estatus");
$sql = $bd -> consultar("TABELA", "$colunas", "codigo = '$getT' AND estatus = 0", false, false, 1);
if($bd -> contaLinha($sql)) {
echo "Contou!";
} else {
echo "Retornou 0!";
}
However, when attempting to execute such codes, the following error is returned:
An error occurred: SQLSTATE [42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
An error occurred: SQLSTATE [42000]: Syntax error or access violation: 1065 Query was empty
And since I'm a beginner in PHPOO, some doubts have arisen:
consultar()
function with no array to test but not
many changes have taken place. I believe the error is not in the class. O
what can I be doing wrong? private
or public
in function __construct()
or
leave it as it is? Thanks!