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

时间:2016-02-27 22:38:49

标签: php pdo

在这段代码中他给我这个问题:

  

致命错误:无法将类PDOStatement的对象转换为第96行的C:\ xampp \ htdocs \ myjob \ like_unlike \ index.php中的字符串

这是第96行:

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

这是完整的代码,如何解决这个问题?

    <!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(), "\n";
            }

            $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 个答案:

答案 0 :(得分:0)

// 1) prepare the statement
$rate_all_count = $DB_con->prepare('SELECT COUNT(*) FROM  wcd_yt_rate WHERE id_item=:iid')
// 2) execute it (with some parameters)
$rate_all_count->execute( array(':iid'=>$pageID) );
// 3) fetch the result
//  - in this case it's just one row having one column
//    so, let's shift the element from the array returned by stmt->fetch()
//    and assign it to a variable
$rate_all_count_value = array_shift($rate_all_count->fetch(PDO::FETCH_NUM));

// 4) do something with the value
echo $rate_all_count_value;

你试图回应语句对象,跳过了第3步。

另见:http://docs.php.net/pdo.prepared-statements