user2759806
user2759806

Reputation: 1

Result in decimals?

Alright so I've been trying to make two values in two different tables add up to one number. Its for posting and comments how many a user has done. The .$Author['id']. reads from another php file and is fine how it is.

When I use the code below it all works but the numbers are in decimals. So if $result has a value of 4 and $result2 is 2 it would come up saying 0.6. I have made sure that there is nothing wrong with the results or num_rows by replacing the echo with echo "$num_rows"; and it comes up with the right result. But why do when I try to add these together they become a decimal?

<?php

$link = mysql_connect("--------", "---------", "--------");
mysql_select_db("--------", $link);

$result = mysql_query("SELECT * FROM post_threads WHERE author = '".$Author['id']."'", $link);
$num_rows = mysql_num_rows($result);

$result2 = mysql_query("SELECT * FROM post_comments WHERE userid = '".$Author['id']."'", $link);
$num_rows2 = mysql_num_rows($result2);

$a = array(".$num_rows." + ".$num_rows2.");
echo "" . array_sum($a) . "\n";

?>

Upvotes: 0

Views: 104

Answers (2)

japborst
japborst

Reputation: 45

Not sure what you meant with the last 2 lines of your code, but I suggest the following:

$a = array($num_rows, $num_rows2);
echo array_sum($a) . "\n";

Hope that works out for you

Upvotes: 1

Emilio Gort
Emilio Gort

Reputation: 3470

You have to change this line:

$a = array(".$num_rows." + ".$num_rows2.");
echo "" . array_sum($a) . "\n";

to:

$a =$num_rows + $num_rows2;
echo $a. "\n";

if you want to use an array, (I dont know why), should be:

$a = array($num_rows,$num_rows2);
echo array_sum($a) . "\n";

Closing the variables in quotes, you are converting them to string

Upvotes: 1

Related Questions