Equivalent of mysql_result in PDO

Asked

Viewed 255 times

1

I’m trying to move from mysql to Pdo but do not understand how to step that mysql_result to Pdo, I appreciate any help made 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;
  • I haven’t tested but assuming you already have the PDO configured try the following: $limite = 10;
$stmt = $conn->prepare("SElECT COUNT('id') FROM anuncios WHERE categoria=:categoria AND estado=1");
$stmt->bindValue(':categoria', $categoria);
$stmt->execute();
$result = ceil($stmt->fetchColumn() / $limite);
$pg = (isset($_GET["pg"])) ? (int)$_GET["pg"] : 1;
$start = ($pg -1) * $limite;

  • He spoke about configuring PDO, I did not configure anything, it is necessary some configuration in xampp or any online storage server?

  • Yes, you need to enable two extensions in your php.ini {extension=php_pdo.dll - extension=php_pdo_mysql.dll} and then establish your connection where you wish ($con = new PDO("mysql:host=localhost;dbname=exercicio", "root", "senha"); ) On this site you can find more information if you have any questions: http://ademir.winponta.com.br/php-pdo-mysql-carregando-configurando/

  • Thanks for the information, I only have to configure in case I use local server, when I upload the site to a host, I have to make a change or normal servers support Pdo?

  • I believe that almost all accept PDO. Otherwise you can contact the support of the chosen hosting for more information...

  • Okay, thank you so much for your help

Show 1 more comment

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;
  • Okay, thank you so much for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.