mysql_result equivalent in PDO

1

I am trying to pass from mysql to pdo but I do not understand how to pass that mysql_result to pdo, I appreciate any help available

$limite = 10;
$SQL_COUNT = mysql_query("SElECT COUNT('id') FROM anuncios WHERE categoria='$categoria' AND estado=1");
$SQL_RESULT = ceil(mysql_result($SQL_COUNT, 0) / $limite);
$pg = (isset($_GET["pg"])) ? (int)$_GET["pg"] : 1;
$start = ($pg -1) * $limite;
    
asked by anonymous 01.04.2015 / 18:54

1 answer

1

In PDO you can get the count result as follows:

$sql = "SElECT COUNT('id') FROM anuncios WHERE categoria=:categoria AND estado=1"; 

$result = $con->prepare($sql); 

$result->execute(array(':categoria' => $categoria)); 

$contagem = $result->fetchColumn();

echo $contagem; 

The above can still be synthesized for the below if we are not dealing with user data:

$sql = "SElECT COUNT('id') FROM anuncios WHERE categoria='bubu' AND estado=1"; 

$contagem = $pdo->query($sql)->fetchColumn(); 

echo $contagem;
    
01.04.2015 / 19:16