Jump to content

Using COUNT in a JavaScript equation - help please


melting_dog

Recommended Posts

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

 

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'] ?>";

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.