|
|
|
|
![]() ![]() |
Sep 29 2007, 11:18 PM
Post
#1
|
|
|
Sparkx Group: [HOSTED] Posts: 354 Joined: 11-October 06 From: Dana Point, CA, USA Member No.: 16,496 |
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, Sparkx Also: I have no clue if it is just my browser or what but my post breaks to boarder for some odd reason? This post has been edited by sparkx: Sep 29 2007, 11:19 PM |
|
|
|
Sep 30 2007, 08:52 AM
Post
#2
|
|
|
Super Member Group: [HOSTED] Posts: 763 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
I'm not really sure what you want but if it is to simply get the results of a math operation try this:
CODE <?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, |
|
|
|
Oct 1 2007, 10:55 PM
Post
#3
|
|
|
Sparkx Group: [HOSTED] Posts: 354 Joined: 11-October 06 From: Dana Point, CA, USA Member No.: 16,496 |
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 |
|
|
|
Oct 2 2007, 02:48 AM
Post
#4
|
|
|
Member - Active Contributor Group: Members Posts: 88 Joined: 5-April 07 From: Cusco - Peru Member No.: 21,283 |
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! |
|
|
|
Oct 2 2007, 04:30 AM
Post
#5
|
|
|
Super Member Group: [HOSTED] Posts: 763 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
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: CODE <?php eval("$"."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, |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 11th October 2008 - 05:03 AM |