Jump to content
xisto Community
sparkx

Php Math Error

Recommended Posts

I cant get php to do simple math. Anyway I need to run a math problem from the mysql database (dont ask why). So lets say I have an entrie: 5+5. I get it from my db then eval it but nothing happens? How could I make it so that it runs the 5+5 rather then just displaying it?Thanks,SparkxAlso: I have no clue if it is just my browser or what but my post breaks to boarder for some odd reason?

Edited by sparkx (see edit history)

Share this post


Link to post
Share on other sites

I'm not really sure what you want but if it is to simply get the results of a math operation try this:

<?php// math with 2 columns$sql="select (numeric_col1+numeric_col2) as result from table";$row=mysql_query($sql);$rs_row=mysql_fetch_array($row);echo $rs_row["result"];?>
The above code only works if you want to perform the math operation with two columns, so, tell me, your entrie is like this or you have it in one column only.

Also you can use the MySql CAST() function.

Best regards,

Share this post


Link to post
Share on other sites

What I want is here is one column: 5+5 and I simply:echo($row['Column']);and that displayes 10 not 5+5. So the echo is:10I don't really know how else to say it. Im sorry if I am still a little confusing.Thanks,Sparkx

Share this post


Link to post
Share on other sites

What I want is here is one column: 5+5 and I simply:echo($row['Column']);
and that displayes 10 not 5+5. So the echo is:
10
I don't really know how else to say it. Im sorry if I am still a little confusing.
Thanks,
Sparkx


If you want PHP math:
echo $row['Column']+5;

If you want PHP + MySQL math:
$rs = mysql_query("select 5+".5);
print_r(mysql_fetch_row($rs));

If you want MYSQL math:
Select 5+5

More on MySQL math.

Blessings!

Share this post


Link to post
Share on other sites

What I want is here is one column: 5+5 and I simply:

echo($row['Column']);

and that displayes 10 not 5+5. So the echo is:

10

I don't really know how else to say it. Im sorry if I am still a little confusing.

Thanks,

Sparkx

Well, it took me more time than i expected but finally i got the solution, the way i got it is with the help of the php eval() function. What this function do is to evaluate the string given as a parameter as it is PHP code so you can execute it, i think it is very similar to the javascript eval() function.

 

So please try the following code and tell us if it is what you want:

 

<?phpeval("$"."variable"."=".$row['Column'].";");echo "\$variable is = $variable";//echo "<script>var var1=eval('$variable'); alert(var1);</script>";?>
The last line -in case you need it- shows the use of the Javascript eval() function, simply uncomment this line to see it in action.

 

Best regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.