以下是我的sql代码
<?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);
?>
我尝试用以下代码替换此代码,但我收到多个错误
<?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);
?>
我的代码Warning: PDO::query() expects parameter 1 to be string, object given line 32, 34,37,39
已编辑的代码
<!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>
上面编辑后我收到以下错误
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
答案 0 :(得分:2)
使用预准备语句与使用纯SQL查询不同。有关此问题,请参阅PHP documentation。有很好的例子如何使用预准备语句和绑定参数。例如,检索不喜欢计数应如下所示:
$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();
使用绑定参数还可以避免SQL注入威胁。
答案 1 :(得分:0)
您正在使用$stmt = $db->prepare()
,然后使用PreparedStatement对象调用query($stmt,...)
。这在PDO中是错误的。
您需要在每个PreparedStatement对象上调用$stmt->execute()
方法。阅读本文以获得解释和示例。 http://php.net/manual/en/pdo.prepare.php
然后,当您拨打$stmt->execute()
时,您需要致电$stmt->fetch()
以检索结果。请注意,您的SELECT COUNT(*) FROM ...
个查询都会返回一行,其中包含一列,因此您需要仅为这些查询调用$stmt->fetch()
。阅读本文,尤其是示例3. http://php.net/manual/en/pdo.prepared-statements.php
从行中只获取一列的有效快捷方式是$stmt->fetchColumn(0)
。读这个。 http://php.net/manual/en/pdostatement.fetchcolumn.php但请注意,不要仅对多行结果集使用此调用一次,或者可以在$stmt
中保留部分未获取的结果集。读这个。 http://php.net/manual/en/pdostatement.closecursor.php