melting_dog Posted July 6, 2010 Share Posted July 6, 2010 Hi. I need to use the total count of items from a table in an equation in javascript. This is as close as I have gotten: $sql = "SELECT COUNT(mailingID) FROM mailinglist"; $result = mysql_query($sql); var sum= "<?= $result ?>"; var sum returns the correct number but preceded by Resource id# (i.e. Resource id #5) and I cannot use this for equations. Any one got any ideas on how to just get the number? Cheers Link to comment https://forums.phpfreaks.com/topic/206882-using-count-in-a-javascript-equation-help-please/ Share on other sites More sharing options...
bh Posted July 6, 2010 Share Posted July 6, 2010 Hi, try this: var sum= "<?= mysql_num_rows($result); ?>"; Link to comment https://forums.phpfreaks.com/topic/206882-using-count-in-a-javascript-equation-help-please/#findComment-1081863 Share on other sites More sharing options...
Mchl Posted July 6, 2010 Share Posted July 6, 2010 Hi, try this: var sum= "<?= mysql_num_rows($result); ?>"; That will always return 1 however... Try this instead: <?php $sql = "SELECT COUNT(mailingID) AS cnt FROM mailinglist"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); ?> var sum= "<?= $row['cnt'] ?>"; Link to comment https://forums.phpfreaks.com/topic/206882-using-count-in-a-javascript-equation-help-please/#findComment-1081866 Share on other sites More sharing options...
bh Posted July 6, 2010 Share Posted July 6, 2010 Yeah i was careless. i didnt see COUNT(... Link to comment https://forums.phpfreaks.com/topic/206882-using-count-in-a-javascript-equation-help-please/#findComment-1081870 Share on other sites More sharing options...
melting_dog Posted July 6, 2010 Author Share Posted July 6, 2010 Thanks! This worked: <?php $sql = "SELECT COUNT(mailingID) AS cnt FROM mailinglist"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); ?> var sum= parseInt ("<?= $row['cnt'] ?>"); with the parseInt Thanks for your help Link to comment https://forums.phpfreaks.com/topic/206882-using-count-in-a-javascript-equation-help-please/#findComment-1081872 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.