简体   繁体   中英

php & pdo & mysql countrows returning 0

I have now spent several hours without getting anywhere. I want to display number of rows in my query and here here is my code:

 <?php 

        $user_id = 1;


        $statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
        $statement->execute(array(':id_user_profile'=> $id_user_profile));


        $rResult = $statement->fetchAll();
        echo count($rResult);
        echo "<h2>Interesi</h2>";
        while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){

            ?>
                <div class="interest" id = "int<?php echo $row['id'];?>">
                    <span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
                    <?php echo $row['name'];?>
                </div>
            <?php
        }


    ?>

And i getting number 10 but now im not getting any interests displayed. If you use this code now:

<?php 

    //$user_id = 1;


        $statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
        $statement->execute(array(':id_user_profile'=> $id_user_profile));
        echo "<p>"  . $statement->rowCount(). " interests:</p>";


        while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){

            ?>
                <div class="interest" id = "int<?php echo $row['id'];?>">
                    <span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
                    <?php echo $row['name'];?>
                </div>
            <?php
        }


    ?>

And now im getting all the interests displayed but it counts: 0 interests. Any ideas who i can count number of interests?

If you read the documentation for PDOStatement::rowCount you will see this line

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.

The work around is to count() function from php like you did. If that did not work then use COUNT(*) as result_count . then when you fetch read that value.


And i getting number 10 but now im not getting any interests displayed

problem

The reason why the result aren't displaying is because you fetched all the row already so you cannot use fetch() again.

solution

Use the array returned by fetchAll() instead and use a foreach instead

$rows = $statement->fetchAll();
echo "<p>"  . count($rows). " interests:</p>";

foreach ($rows as $row){
//echo $row['name']
}
<?php 

    //$user_id = 1;


        $statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
        $statement->execute(array(':id_user_profile'=> $id_user_profile));
        echo "<p>"  . $statement->rowCount(). " interests:</p>";
        $counter = 0; //declare counter

        while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
                 $counter++; //increment counter
            ?>
                <div class="interest" id = "int<?php echo $row['id'];?>">
                    <span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
                    <?php echo $row['name'];?>
                </div>
            <?php
        }


    ?>

[NOTE: Edit by @DinoAmino removed the emphasis characters around the two uses of $counter , which caused the code to not compile for the question asker]

I solved my problem like this:

Here is the updated solving version. Thank you all for the input:

<?php 

    $user_id = 1;

    $statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
    $statement->execute(array(':id_user_profile'=> $id_user_profile));

    $rows = $statement->fetchAll();
    if(count($rows) >= '1') {

    ?>
        <div class="your-interests">
            <?php

            echo "<p>"  . count($rows). " interests:</p>";
            foreach ($rows as $row){

                ?>
                    <div class="interest" id = "int<?php echo $row['id'];?>">
                        <span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
                        <?php echo $row['name'];?>
                    </div>
                <?php
            }


            ?>
        </div>
    <?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM