I have a table called votes, in this table I want to select two values from different columns, but the values are in the same row of course. Here is an example of a row I want to select 2 values out of:
id user_id poll_id answer_id
8 49 1 1
I want to select the value from the column user_id and poll_id Here is the code I have written for it using PHP and PDO:
$voteCheck = $db->prepare('SELECT * FROM votes WHERE user_id=:user_id, poll_id=:poll_id');
$voteCheck->execute(array(':user_id' => $user_id, ':poll_id' => $poll_id));
$voteRow = $voteCheck->fetch(PDO::FETCH_ASSOC);
$check_user_id = $voteRow['user_id'];
$check_poll_id = $voteRow['poll_id'];
echo "Hello";
echo $check_user_id;
echo $check_poll_id;
I am new to PHP so there might be some stupid things I did here, but I wanted to bind the variable $user_id to the column name user_id, the same goes for the $poll_id and the row poll_id. The numbers in my example row above the code are already existent in my table, and I want to add an if statement which checks if the variable numbers are already existent in my database. I am making a poll system and a user is linked with the poll, and I want to prevent that they can vote multiple times. In my mind the if statement would seem like this:
if (empty($check_user_id) and empty(check_poll_id)) {
*INSERT NEW USER ID AND POLL_ID IN TABLE*
} else {
echo "You already voted for this poll";
}
But the statement above doesn't work if the SELECT query doesn't work, is there another way to select values from multiple columns in the same row using PDO? Hopefully you understand my question.
Aucun commentaire:
Enregistrer un commentaire