gpt4 book ai didi

php - 将旧的 sql 代码替换为 pdo 后出现多个错误

转载 作者:行者123 更新时间:2023-11-29 06:15:54 25 4
gpt4 key购买 nike

以下是我的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);
?>

我的代码中出现错误 警告:PDO::query() 期望参数 1 为字符串,第 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

最佳答案

使用准备好的语句与使用普通 SQL 查询不同。请引用PHP documentation对这个。有很好的例子说明如何使用准备好的语句和绑定(bind)参数。例如,检索不喜欢计数应该如下所示:

$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();

使用绑定(bind)参数还可以让您避免 SQL 注入(inject)威胁。

关于php - 将旧的 sql 代码替换为 pdo 后出现多个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35970125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com