簡體   English   中英

在PHP中將代碼從MySQL轉換為PDO的問題

[英]issue with converting code from MySQL to PDO in PHP

我有一段代碼(用於)通過表條目計算,以找出在特定問題上投票的數量。

這是表:


  USER   |   answer_id  |  poll_id  |
------------------------------------
usename        5             1
user2          4             1
user3          5             1

等等。 基本上每次用戶投票時,都會生成一個單獨的行,並且answer_id列包含他們投票的問題編號。

在我將代碼從sql更新為PDO語句之后,代碼找到了投票數,停止了工作,並且只查找了最近的投票,因此只返回了一票。 這是我更新的代碼:



// Get Votes
$q = 'SELECT * FROM votes WHERE poll_id = :poll_id';
$params = array(':poll_id' => 1);
$stmt = $pdo->prepare($q);
$stmt->execute($params);
while ($row = $stmt->fetch()) {

$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )

$total_votes = 0;

$answer_id = $row['answer_id'];
$votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particulair answer
$total_votes++;
}

// End Get votes

這是它開始處理這些結果的地方


// Start Get answers

$q = 'SELECT * FROM answers WHERE poll_id = :poll_id';
$params = array(':poll_id' => 1);
$stmt = $pdo->prepare($q);
$stmt->execute($params);
while ($row = $stmt->fetch()) { //loop through all answers this poll has

$id = $row['id']; //the id of the answer -> the amount of votes for each answer we stored in $votes[id_of_answer] so for this id it would be $votes[$id]
$width = round((int)$votes[$id]/$total_votes*99+1); //100% of votes

echo ''.$row['answer'].' 
('.(int)$votes[$id].' vote'.((int)$votes[$id] != 1 ? "s":"").')
'; }

這是MySQL中的原始代碼:


$get_votes = mysql_query("SELECT * FROM votes WHERE poll_id = '$id' "); //select all votes to this poll

$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )

$total_votes = 0;

while($vote = mysql_fetch_assoc($get_votes)) { //retrieve them $answer_id = $vote['answer_id']; $votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particular answer $total_votes++; }

//now loop through the answers and get their corresponding amount of votes from $votes[id_of_answer]

$get_answers = mysql_query("SELECT * FROM answers WHERE poll_id = '$id' ");

while($answer = mysql_fetch_assoc($get_answers)) { //loop through all answers this poll has

$id = $answer['id']; //the id of the answer -> the amount of votes for each answer we stored in $votes[id_of_answer] so for this id it would be $votes[$id] $width = round((int)$votes[$id]/$total_votes*99+1); // 100% of votes

echo ''.$answer['answer'].'
('.(int)$votes[$id].' vote'.((int)$votes[$id] != 1 ? "s":"").')
';

}

誰能告訴我哪里出錯了? 它是//Get Votes部分我正在努力研究我所做的事情。 據我所知,沒有什么是不公正的。

檢查bindparamfetchall而不是fetch。

/* Execute a prepared statement by binding PHP variables */
$poll_id = 1;
$sth = $dbh->prepare('SELECT * FROM votes WHERE poll_id = :poll_id');
$sth->bindParam(':poll_id', $poll_id, PDO::PARAM_INT);
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);

您正在覆蓋您的數組並計入循環:

while ($row = $stmt->fetch()) {

  $votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )

  $total_votes = 0;

  $answer_id = $row['answer_id'];
  $votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particulair answer
  $total_votes++;
}

應該:

$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )
$total_votes = 0;

while ($row = $stmt->fetch()) {
  $answer_id = $row['answer_id'];
  $votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particulair answer
  $total_votes++;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM