0

I have troubles with retrieving data from a distant database. Access with PDO to database is ok, i debug.print the rowcount, it returns several occurences, but the code seems to stop when comes the fetching

        $arr = $reponse->fetchAll(PDO::FETCH_ASSOC);        

Here is the link to see by yourself : http://bacly.fr/baclymphp/getTournois.php

I did the ame thing with another db, with same code-like feature, and it's giving me the result

http://bacly.fr/todophp/www/php/getTodo.php

here is my code for getTournois.php :

<?php
require("config.php");

    $query="Select t1.tournois_id as tournois_id,t1.tournois_title as tournois_title, t1.tournois_date_debut as tournois_date_debut, t1.tournois_date_fin as tournois_date_fin, t1.tournois_date_limite as tournois_date_limite, coalesce(t2.tournois_inscriptions,0) as tournois_inscriptions, t1.tournois_description as tournois_description,  t1.tournois_simple as tournois_simple, t1.tournois_double as tournois_double, t1.tournois_mixte as tournois_mixte ";
    $query.= " From ";
    $query.= " (select t.tournois_id AS tournois_id, t.tournois_title AS tournois_title, t.tournois_date_debut AS tournois_date_debut, t.tournois_date_fin AS tournois_date_fin, t.tournois_date_limite AS tournois_date_limite, t.tournois_description as tournois_description, t.tournois_simple as tournois_simple, t.tournois_double as tournois_double, t.tournois_mixte as tournois_mixte FROM jnew_tournois_tournois t";
    $query.=" order by tournois_date_limite DESC) t1";
    $query.= " left join";
    $query.= " (select count(ti.tournois_inscriptions_id) as tournois_inscriptions,ti.tournois_inscriptions_tid as tournois_id from jnew_tournois_tournois_inscriptions  ti) t2";
    $query.= " ON t1.tournois_id = t2.tournois_id order by t1.tournois_date_limite desc";

    debug.print($query);

    try
    {
        $bdd = new PDO($db_config['SGBD'] .':host='. $db_config['HOST'] .';dbname='. $db_config['DB_NAME'], $db_config['USER'], $db_config['PASSWORD'], $db_config['OPTIONS']);
        $reponse = $bdd->prepare($query);
        $reponse->execute();
        debug.print("rowcount");
        debug.print($reponse->rowcount());

        if ($reponse->rowcount() >0){
            debug.print('fetching');
            $arr = $reponse->fetchAll(PDO::FETCH_ASSOC);        
            debug.print($arr);

        }

    }
    catch(Exception $e)
    {
        // En cas d'erreur, on affiche un message et on arrête tout
        $result.=$query . "<br>" . $e->getMessage();
        debut.print($result);   
    }
    $bdd=null;

echo $result = json_encode($arr);

?>
Nightf
  • 61
  • 1
  • 7
  • 1
    1) Why do you write `select t.tournois_id AS tournois_id, t.tournois_title AS tournois_title, ...` ? The alias is the same as the column name so this isn't necessary. 2) You print `$arr` before initializing it. – Xebax Aug 07 '15 at 22:04
  • 1) Yes, you're right, but that's an old code i am reusing for a mobile app (m first one), and it's working so i didnt try to improve it. 2) indeed, i fixed it abd put it after,you can see online, it gives Array, and nothing more – Nightf Aug 07 '15 at 22:22
  • Are you sure your query does return some rows ? What do you get when you execute it directly on the database ? – Xebax Aug 07 '15 at 22:27
  • Does it throw any exceptions? – RisingSun Aug 07 '15 at 22:44
  • Can you also check if it is returning either an empty array or `FALSE`? – RisingSun Aug 07 '15 at 22:50
  • i checked my query with phpmyadmin it returns some rows as expected – Nightf Aug 07 '15 at 23:30
  • i think it's an array, as it puts me Array when i debug.print $arr – Nightf Aug 07 '15 at 23:55
  • in your example link, the one that shows the full query, I see **pas derreur** at the end. What is that about? – RisingSun Aug 10 '15 at 18:37
  • its just french text i added to debug.print in the try part of the PDO, meaning there is no error – Nightf Aug 11 '15 at 16:33

1 Answers1

0

I think your code works as expected. I think the part that is misleading you into thinking that it does not is $reponse->rowcount().

From PHP Docs,

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

So I think you should switch to a different row counting method to see if that changes the count. Check out these questions

How to get the number of rows grouped by column?

Row count with PDO

Community
  • 1
  • 1
RisingSun
  • 1,693
  • 27
  • 45
  • Well, i think so my code works, but at least i should get some json data on screen. With the other example written with the same code but sql query and dbname (same provider tho), i have it : http://bacly.fr/todophp/www/php/getTodo.php – Nightf Aug 07 '15 at 23:43
  • if i i follow your answer, if i remove the test on rowcount() and due to the fact i have a result, i should have some stuff in json at the end, but i don't get anything – Nightf Aug 07 '15 at 23:54
  • Another thing is that it works perfectly on my local. – Nightf Aug 07 '15 at 23:56