在PHP中将代码从MySQL转换为PDO的问题

时间:2013-02-27 18:45:11

标签: php mysql database pdo

我有一段代码(用于)通过表条目计算,以找出在特定问题上投票的数量。

这是表格:


  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部分我努力研究我所做的事情。据我所知,没有任何不公正。

2 个答案:

答案 0 :(得分:1)

结帐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);

答案 1 :(得分:1)

你正在覆盖你的数组并计入循环:

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++;
}