1
I’m trying to make a rowCount using a class I developed for the connection and some functions involving the 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 trying to execute such codes, the following error is returned:
Error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
Error occurred: SQLSTATE[42000]: Syntax error or access Violation: 1065 Query was Empty
And since I’m a beginner in PHPOO, some questions have arisen:
- I’ve tried using the function
consultar()
without array to test but not many changes have occurred. I believe the error is not in the class. The I might be doing wrong? - Should I use
private
orpublic
infunction __construct()
or leave as is?
Thank you!
Yes, but before using the class I was doing the connection function and rowCount directly and it worked perfectly.
– Igor
I added a few more details
– Skywalker
I noticed this, and then I tried $bd -> accountLine ("SELECT pname, email, test, code , statues from TABLE WHERE code = '$Gett' AND statues = 0") but it didn’t work either...
– Igor
Generated what error like this? column name is pname or name? why la in description Voce uses name
– Skywalker
I did so: $sql = "SELECT pname, email, test, code, status from TABLE WHERE code = '$Gett' AND status = 0"; $bd -> countLine($sql); E generated: Fatal error: Call to Undefined method PDO::rowCount()...
– Igor
I tested the code and that error here
Ocorreu um erro: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'Retornou 0!
is why this by passing the variable $columns between quotes to the function query.– Skywalker
and if $columns is NULL or different from Voce array need to treat.
– Skywalker
So in case I have to use the function consult along with the countLine?
– Igor
Tell me one thing in the query Voce wants that returns all the data from the right database?
– Skywalker
I’m sorry up there, I had put an old code in here, but the correct one is a name and not a name... Regarding the query I want him to make the query and do the rowCount so I can know if the return of someone with code = '$Gett' AND statute = 0" is greater than 0 or equal to 0 and so do other checks after this.
– Igor
I added the solution, I hope it’s what you need, the parts I changed I left commented.
– Skywalker
Thanks for the support, @Abraham. It worked!
– Igor