dourao3960
dourao3960
2016-02-27 22:38

致命错误:无法转换类PDOStatement的对象

  • php
  • pdo

In this code he give me this probleme :

fatal error: Object of class PDOStatement could not be converted to string in C:\xampp\htdocs\myjob\like_unlike\index.php on line 96

This is line 96:

<div class="rate-count"><?php echo $rate_all_count; ?></div> 

And this is the full code, how can fix this problem ??

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>WebCodo :: Like & Dislike System With jQuery Ajax and PHP</title>

            <link type="text/css" rel="stylesheet" href="css/style.css">
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


        </head>

        <body>

        <?php
        include 'config.php';
        $user_ip = $_SERVER['REMOTE_ADDR'];
        $pageID = '38'; // The ID of the page, the article or the video ...

        //function to calculate the percent
        function percent($num_amount, $num_total) {
            @$count1 = $num_amount / $num_total;
            $count2 = $count1 * 100;
            $count = number_format($count2, 0);
            return $count;
        }

        // check if the user has already clicked on the unlike (rate = 2) or the like (rate = 1)
            $dislike_sql = $DB_con->prepare('SELECT COUNT(*) FROM  wcd_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 2 ');
            $dislike_sql->execute();   
            //$dislike_count = mysql_result($dislike_sql, 0); 

            $like_sql = $DB_con->prepare('SELECT COUNT(*) FROM  wcd_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 1 ');
            $like_sql->execute();         
            //$like_count = mysql_result($like_sql, 0);  

            // count all the rate 
            $rate_all_count = $DB_con->prepare('SELECT COUNT(*) FROM  wcd_yt_rate WHERE id_item = "'.$pageID.'"');
            try {
                $rate_all_count->execute();
            } catch (Exception $e) {
                echo 'Exception : ',  $e->getMessage(), "
";
            }

            $rate_like_count = $DB_con->prepare('SELECT COUNT(*) FROM  wcd_yt_rate WHERE id_item = "'.$pageID.'" and rate = 1');
            $rate_like_count->execute(); 
            $rate_like_percent = percent($rate_like_count, $rate_all_count);

            $rate_dislike_count = $DB_con->prepare('SELECT COUNT(*) FROM  wcd_yt_rate WHERE id_item = "'.$pageID.'" and rate = 2');
            $rate_dislike_count->execute();        
            $rate_dislike_percent = percent($rate_dislike_count, $rate_all_count);
    ?>

    <script>
        $(function(){ 
            var pageID = <?php echo $pageID;  ?>; 

            $('.like-btn').click(function(){
                $('.dislike-btn').removeClass('dislike-h');    
                $(this).addClass('like-h');
                $.ajax({
                    type:"POST",
                    url:"ajax.php",
                    data:'act=like&pageID='+pageID,
                    success: function(){
                    }
                });
            });
            $('.dislike-btn').click(function(){
                $('.like-btn').removeClass('like-h');
                $(this).addClass('dislike-h');
                $.ajax({
                    type:"POST",
                    url:"ajax.php",
                    data:'act=dislike&pageID='+pageID,
                    success: function(){
                    }
                });
            });
            $('.share-btn').click(function(){
                $('.share-cnt').toggle();
            });
        });
    </script>



        <div class="tab-cnt">

            <div class="tab-tr" id="t1">
                <div class="like-btn <?php if($like_count == 1){ echo 'like-h';} ?>">Like</div>
                <div class="dislike-btn <?php if($dislike_count == 1){ echo 'dislike-h';} ?>"></div>


                <div class="stat-cnt">
                    <div class="rate-count"><?php echo $rate_all_count; ?></div>
                    <div class="stat-bar">
                        <div class="bg-green" style="width:<?php echo $rate_like_percent; ?>%;"></div>
                        <div class="bg-red" style="width:<?php echo $rate_dislike_percent; ?>%"></div>
                    </div><!-- stat-bar -->
                    <div class="dislike-count"><?php echo $rate_dislike_count; ?></div>
                    <div class="like-count"><?php echo $rate_like_count; ?></div>
                </div><!-- /stat-cnt -->
            </div><!-- /tab-tr -->

            </div><!-- /share-cnt -->


        </div><!-- /tuto-cnt -->





    </body>
    </html>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答