Reputation: 993
following is my sql code
<?php
include 'config.php';
$user_ip = $_SERVER['REMOTE_ADDR'];
$pageID = '33'; // 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 = mysql_query('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 2 ');
$dislike_count = mysql_result($dislike_sql, 0);
$like_sql = mysql_query('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 1 ');
$like_count = mysql_result($like_sql, 0);
// count all the rate
$rate_all_count = mysql_query('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item = "'.$pageID.'"');
$rate_all_count = mysql_result($rate_all_count, 0);
$rate_like_count = mysql_query('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item = "'.$pageID.'" and rate = 1');
$rate_like_count = mysql_result($rate_like_count, 0);
$rate_like_percent = percent($rate_like_count, $rate_all_count);
$rate_dislike_count = mysql_query('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item = "'.$pageID.'" and rate = 2');
$rate_dislike_count = mysql_result($rate_dislike_count, 0);
$rate_dislike_percent = percent($rate_dislike_count, $rate_all_count);
?>
I tried to replace this code with following code but i get multiple error
<?php
include 'config.php';
$user_ip = $_SERVER['REMOTE_ADDR'];
$pageID = '33'; // 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->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 2 ');
$dislike_count = $db->query($dislike_sql, 0);
$like_sql = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 1 ');
$like_count = $db->query($like_sql, 0);
// count all the rate
$rate_all_count = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item = "'.$pageID.'"');
$rate_all_count = $db->query($rate_all_count, 0);
$rate_like_count = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item = "'.$pageID.'" and rate = 1');
$rate_like_count = $db->query($rate_like_count, 0);
$rate_like_percent = percent($rate_like_count, $rate_all_count);
$rate_dislike_count = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item = "'.$pageID.'" and rate = 2');
$rate_dislike_count = $db->query($rate_dislike_count, 0);
$rate_dislike_percent = percent($rate_dislike_count, $rate_all_count);
?>
ERROR in my code Warning: PDO::query() expects parameter 1 to be string, object given line 32, 34,37,39
EDITED CODE
<!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>
<div class="webcodo-top" >
<a href="http://webcodo.com/like-dislike-system-with-jquery-ajax-and-php-youtube-like-design">
<div class="wcd wcd-tuto"> < Come back to the tuto page</div>
</a>
<a href="http://webcodo.com">
<div class="wcd wcd-logo">WEBCODO</div>
</a>
<div class="wcd"></div>
</div>
<?php
include 'config.php';
$user_ip = $_SERVER['REMOTE_ADDR'];
$pageID = '33'; // 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_stmt = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip = :user_ip and id_item = :item_id and rate = 2;');
$dislike_stmt->execute(array(':user_ip' => $user_ip, ':item_id' => $pageID));
$dislike_count = $dislike_stmt->fetchColumn();
$like_sql = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip =:user_ip and id_item =:item_id and rate = 1 ');
$like_sql->execute(array(':user_ip' => $user_ip, ':item_id' => $pageID));
$like_count = $like_sql->fetchColumn();
// count all the rate
$rate_all_count = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item =:item_id');
$rate_all_count->execute(array(':item_id' => $pageID));
$rate_all_count = $rate_all_count->fetchColumn();
$rate_like_count = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item =:item_id and rate = 1');
$rate_like_count->execute(array(':item_id' => $pageID));
$rate_like_percent = $rate_like_count->fetchColumn();
$rate_dislike_count = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE id_item =:item_id and rate = 2');
$rate_dislike_count->execute(array(':item_id' => $pageID));
$rate_dislike_percent = $rate_dislike_count->fetchColumn();
$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">
<h1>Youtube Like & Dislike System With PHP, jQuery & Ajax</h1>
<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="share-btn">Share</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 class="share-cnt">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style ">
<a class="addthis_button_linkedin_counter"></a>
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
<a class="addthis_button_pinterest_pinit"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
</div><!-- /share-cnt -->
</div><!-- /tuto-cnt -->
</body>
</html>
after above edit i get following error
Notice: Object of class PDOStatement could not be converted to int in C:\Users\sanoj\Documents\NetBeansProjects\like youtube\index.php on line 26
Call Stack
# Time Memory Function Location
1 0.0020 254504 {main}( ) ..\index.php:0
2 0.0260 291496 percent( ) ..\index.php:50
( ! ) Warning: Division by zero in C:\Users\sanoj\Documents\NetBeansProjects\like youtube\index.php on line 26
Call Stack
# Time Memory Function Location
1 0.0020 254504 {main}( ) ..\index.php:0
2 0.0260 291496 percent( ) ..\index.php:50
( ! ) Notice: Object of class PDOStatement could not be converted to int in C:\Users\sanoj\Documents\NetBeansProjects\like youtube\index.php on line 26
Call Stack
# Time Memory Function Location
1 0.0020 254504 {main}( ) ..\index.php:0
2 0.0370 295088 percent( ) ..\index.php:55
( ! ) Warning: Division by zero in C:\Users\sanoj\Documents\NetBeansProjects\like youtube\index.php on line 26
Call Stack
# Time Memory Function Location
1 0.0020 254504 {main}( ) ..\index.php:0
2 0.0370 295088 percent( ) ..\index.php:55
Upvotes: 1
Views: 61
Reputation: 2817
Using prepared statements is not the same as using plain SQL queries. Please, refer to PHP documentation about this. There are good examples how to use prepared statements and binding parameters. For instance, retrieving dislikes count should look like this:
$dislike_stmt = $db->prepare('SELECT COUNT(*) FROM wcd_yt_rate WHERE ip = :user_ip and id_item = :item_id and rate = 2;');
$dislike_stmt->execute(array(':user_ip' => $user_ip, ':item_id' => $pageID));
$dislike_count = $dislike_stmt->fetchColumn();
Using binding parameters also would allow you avoiding SQL injection threats.
Upvotes: 2
Reputation: 108736
You're using $stmt = $db->prepare()
, then calling query($stmt,...)
with the PreparedStatement object. That's wrong in PDO.
You need to call the $stmt->execute()
method on each PreparedStatement object. Read this for an explanation and example. http://php.net/manual/en/pdo.prepare.php
Then when you've called $stmt->execute()
, you need to call $stmt->fetch()
to retrieve the results. Notice that your SELECT COUNT(*) FROM ...
queries all return one row with one column in it, so you need to call $stmt->fetch()
just for those queries. Read this, especially Example 3. http://php.net/manual/en/pdo.prepared-statements.php
A valid shortcut to getting just one column from the row is $stmt->fetchColumn(0)
. Read this. http://php.net/manual/en/pdostatement.fetchcolumn.php But be careful not to use this call just once for multiple-row result sets, or you may leave a partially unfetched resultset in your $stmt
. Read this. http://php.net/manual/en/pdostatement.closecursor.php
Upvotes: 0