Class error, query php, can not be converted to whole

2

Hi, I'm doing a page through php but I have a problem when I want to count the number of records and I can not use it to get the number of pages.

Php code:

<?php

require_once 'database.php';
$database_connection = database_connect();
$title='hola';

$content='<div>';

//user input
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 50 ? (int)$_GET['per-page'] : 5;

//Positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$art = $database_connection->query("SELECT id FROM coffee");
//Query
$articles = $database_connection->prepare("SELECT id FROM coffee LIMIT {$start},{$perPage}");

$articles->execute();
$articles = $articles->fetchAll();

$total = $database_connection->query("SELECT COUNT(*) FROM coffee");

//pages
$pages = ($total/$perPage);


$content .= '<div>';


include 'Template_1.php';
?>

Basically my question is here $pages = ($total/$perPage); how could the conversion be so that when I do the query:

$total = $database_connection->query("SELECT COUNT(*) FROM coffee"); 

is an integer and not a resultset.

    
asked by Perl 13.09.2016 в 22:43
source

1 answer

3

Without knowing exactly what $database_connection is, I'm going to risk guessing that:

$total = $database_connection->query("SELECT COUNT(*) FROM coffee");

what is returning $database_connection->query is a resultset and not a specific value, so $total will not contain an integer, but a result that you will still have to process.

If you are using PDO, you could do something like this (I have not tried it and it may fail):

$resultado = $database_connection->query("SELECT COUNT(*) AS total FROM coffee");
$fila = $resultado->fetch(PDO::FETCH_ASSOC);
$total = $fila["total"];

$pages = ($total/$perPage);
    
answered by 13.09.2016 в 22:52