So I’m racking my brain and I’ve been working on this for a few hours and I just keep getting 0’s returned back when I know I should be getting the data I’m asking for. It’s possible I’m just missing something but for the life of me I can’t figure it out.
I’ve tried WHILE, FOR and FOREACH statements, all to no avail. I’m actually so frustrated I debated punching my computer…which is a thought that has never crossed my mind before…so you know I’m mad (HAHA, so is the life of a developer).
Anyway, here is my function code.
[php]
function getVotes($songid) {
$host = 'localhost';
$db = 'db';
$user = 'db';
$pass = 'db';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->prepare('SELECT * FROM tblsongratings WHERE songid='.$songid);
$stmt->execute();
$upv = 0;
$dwv = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['upv'] == 1) {
return $upv++;
} else if ($row['dwv'] == 1){
return $dwv++;
}
}
return '<small><i class="fa fa-thumbs-up"></i> ' .$upv . ' <i class="fa fa-thumbs-down"></i> ' . $dwv. "</small>";
$dsn = null;
}
[/php]
Here is my SQL data in case anyone wants to play around with it.
[code]CREATE TABLE IF NOT EXISTS tblsongratings
(
id
int(11) NOT NULL AUTO_INCREMENT,
songid
int(11) NOT NULL,
userid
int(11) NOT NULL,
upv
int(11) NOT NULL,
dwv
int(11) NOT NULL,
ipaddress
varchar(255) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=78 ;
–
– Dumping data for table tblsongratings
INSERT INTO tblsongratings
(id
, songid
, userid
, upv
, dwv
, ipaddress
) VALUES
(75, 5, 1, 1, 0, ‘71.115.150.34’),
(74, 2, 1, 0, 0, ‘71.115.150.34’),
(73, 1, 1, 1, 0, ‘71.115.150.34’),
(76, 10, 1, 0, 0, ‘71.115.150.34’),
(77, 6, 1, 1, 0, ‘71.115.150.34’);[/code]
I’m about ready to wash my hands of this feature. It’s been nothing but trouble. So any help you can provide me would be greatly appreciated.