简体   繁体   中英

issue with converting code from MySQL to PDO in PHP

I have a piece of code which (used to) count through table entries to find out the number of votes cast on a specific question.

here is the table:


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

and so on. Basically everytime a user votes, a seperate row is made and the answer_id column holds the question number they voted on.

After I updated my code from sql to PDO statements, the code to find the number of votes, stopped working, and finds only the most recent vote, thus there is only ever one vote returned. Here is the code i updated:



// 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

Here is where it starts to process those results


// 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":"").')
'; }

And here is the original code in 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":"").')
';

}

Can anyone tell me where I'm going wrong? its the //Get Votes part im struggling on working out what ive done. As far as i can see, theres nothing unjust.

Check out bindparam and fetchall instead of 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);

You are overwriting your array and count in the loop:

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

should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM