Reputation: 4849
Good day, I've been trying to figure out a problem in my code. I'm using SQL_CALC_FOUND_ROWS and FOUND_ROWS() to retrieve the total amount of records in my database (with PDO). My problem is that FOUND_ROWS() always returns 0.
The thing is that I got it working before, but I made some adjustments here and there and now it stopped working. I can't for the love of me remember what I could have deleted that was important and can't seem to find any good documentation using these functions with PDO.
So far I've tried putting it in different loops, tried fetch() with different modes, tried placing the commands in different order (i.e. before the while and after the while loop etc.). I think I'm just missing something very easy here, but I've been staring it this thing for about 1 or 2 hours now and it's driving me mad.
So here's my code:
public function findBerichten($args)
{
//$offset zorg ervoor dat pagina 1 als record 0 in de database zoekt
$offset = ($args['huidigePagina'] - 1) * $args['itemsPerPagina'];
$sth = $this->db->DBH()->prepare("SELECT SQL_CALC_FOUND_ROWS
berichten.berichtID,
berichten.bericht,
berichten.naam,
berichten.mail
FROM `berichten`
ORDER BY berichten.datumToegevoegd DESC
LIMIT ?, ?");
$sth->bindParam(1, $offset, PDO::PARAM_INT);
$sth->bindParam(2, $args['itemsPerPagina'], PDO::PARAM_INT);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
$berichten = array();
while($row = $sth->fetch())
{
$bericht = new Bericht();
$bericht->setBerichtID(htmlentities(strip_tags($row['berichtID'])));
$bericht->setBericht(htmlentities(strip_tags($row['bericht'])));
$bericht->setNaam(htmlentities(strip_tags($row['naam'])));
$bericht->setMail(htmlentities(strip_tags($row['mail'])));
$berichten[] = $bericht;
}
$sth = $this->db->DBH()->prepare("SELECT FOUND_ROWS() as aantalBerichten");
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
$this->aantalBerichten = $sth->fetch();
var_dump($this->aantalBerichten);
return $berichten;
}
index.php
if($huidigePagina < 1)
{
//$huidigePagina is 1
$huidigePagina = 1;
}
//Als de huidige pagina groter is als het totaal aantal pagina's
if($huidigePagina > $totaalPaginas)
{
//$huidigePagina is gelijk aan het totaal aantal pagina's
$huidigePagina = $totaalPaginas;
}
$berichtDAO->findBerichten(array('huidigePagina'=>$huidigePagina, 'itemsPerPagina'=>10))
output var_dump: array(1) { ["aantalBerichten"]=> string(1) "0" }
If you've got any idea's let me know because I'm willing to try just about anything right now :)
Here's to hoping this question isn't too noob! As I said, I think I'm missing something very easy here.
edit
the execute() function is correct etc. when used in if() statement it still proceeds to execute code
Also; im 90% sure this has got to do with the FOUND_ROWS() portion of code.
edit 2, included db class and Bericht() class
Bericht class:
<?php
Class Bericht
{
private $db;
private $berichtID;
private $bericht;
private $naam;
private $mail;
public function __construct(Db $db)
{
$this->db = $db;
}
public function setBerichtID($berichtID)
{
$this->berichtID = $berichtID;
}
public function getBerichtID()
{
return $this->berichtID;
}
public function setBericht($bericht)
{
$this->bericht = $bericht;
}
public function getBericht()
{
return $this->bericht;
}
public function setNaam($naam)
{
$this->naam = $naam;
}
public function getNaam()
{
return $this->naam;
}
public function setMail($mail)
{
$this->mail = $mail;
}
public function getMail()
{
return $this->mail;
}
}
db class:
<?php
class Db
{
//bij het laden van Db
public function __construct()
{
//voer functie connect() uit
$this->connect();
}
//functie voor het verbinding maken met en het selecteren van een database
private function connect()
{
//$connection is connectie naar mysql database (met de opgegeven inlog waardes)
$connection = mysql_connect('localhost', 'user', 'pw');
//als er geen connectie gemaakt kan worden
if(!$connection)
{
//die (voer overige code niet meer uit) en echo string + de mysql error
die("Kan geen verbinding maken: " . mysql_error());
}
//selecteert de opgegeven database met de connectie ($connection)
mysql_select_db("db", $connection);
}
public function DBH()
{
try
{
$DBH = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $DBH;
}
catch (PDOException $except)
{
echo $except->getMessage();
}
}
//function to retrieve records -- not important
public function getRecords($sth)
{
$rows = array();
if($sth->execute() == true)
{
$sth->setFetchMode(PDO::FETCH_OBJ);
while($row = $sth->fetch())
{
$rows[] = $row;
}
return $rows;
}
else
{
return false;
}
}
}
Upvotes: 3
Views: 3589
Reputation: 1402
Not completely sure what is happening inside your DB class, so a few assumptions on this answer.
I assume this was working at one stage on your machine similar code, so trace_mode isnt the answer.
I assume
$this->db->DBH()
is a function which is going off to get a new database handler for use in the database query.
The second time you call the prepare function for DBH it is creating a new database handler which is not storing the information from the other DBH call earlier.
A fix would be to do something like
$dbh = $this->db->DBH();
early in the function, then replace the prepare calls with with
$dbh->prepare("(SQL)");
Edit: Looks like at least my DB->DBH() assumption was correct!
Upvotes: 3
Reputation: 16968
Have you tried setting the MySQL trace mode in PHP?
In PHP 5.2.6, this defaults to On and can cause some errors with FOUND_ROWS()
You can do this using one of three methods:
php.ini:
mysql.trace_mode = 0
PHP:
ini_set("mysql.trace_mode", "0");
.htaccess:
php_value mysql.trace_mode "0"
Alternatively:
I am not sure whether the SQL_CALC_FOUND_ROWS
is being passed to the second PDO session. Have you tried combining them into one query and retrieving multiple recordsets? It is very easy to do
Upvotes: 2