0

I created a database (table) in SQL similar to this.

   x   y   z
1  x1  y1  z1
2  x2  y2  z2

This is a table that I created; now I want to do some math on it using PHP. How can I retrieve the data and perform math on it? How to get data from the DB table and save them in PHP variables?

function ans($x1, $y2, $z2)
{
#a = x1 - y2;
#z2 = $z2 -$a;
}
3
  • are you asking about how to execute select query ?? Commented Jul 20, 2012 at 5:25
  • Which DBMS are you using? How did you create the table and load the data? Commented Jul 20, 2012 at 5:46
  • i am using www.000webhost.com , first i created db in sql than i use phpmyadmin . Commented Jul 20, 2012 at 7:37

2 Answers 2

1

The code will look as follow:

<?php

 $con = mysql_connect("localhost","username","password");
 if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }

mysql_select_db("database", $con);

$xAnswer = 0;
$yAnswer = 0;
$zAnswer = 0;

$result = mysql_query("SELECT x,y,z FROM MathTable");

while($row = mysql_fetch_array($result))
   {
     $xAnswer = Add($row["x"],$row["y"]);
     $yAnswer = Multiply($row["z"],$row["y"]);
     $zAnswer = Subtract($row["z"],$row["x"]);
   }

mysql_close($con);


    function Add($x,$y)
    {
       return $x + $y;
    }

    function Subtract($x,$y)
    {
       return $x-$y;
    }

    function Multiply($x,$y)
    {
       return $x*$y;
    }

    function Divide($x,$y)
    {
       return $x/$y;
        }
 ?>

Alternatively one can handle the math in the sql as well as follow and then just pull the answers into a variable:

SELECT (x+y) AS Add,(x-y) AS Subtract,(z/x) AS Divide FROM MathTable

The the loop through the recordsset will be as follow

while($row = mysql_fetch_array($result))
       {
         $xAnswer = $row["Add"];
         $yAnswer = $row["Subtract"];
         $zAnswer = $row["Divide "];
       }

Remember that the variables will have a different answer everytime the loop increments because of the amount of records in the table so you might want a running total or alternatively you can keep running total inside using the above Add example variables:

$xAnswer = $xAnswer  + $row["Add"];
Sign up to request clarification or add additional context in comments.

3 Comments

Please ensure that all the casting of variables are also taken into account. To do math strings must be coverted to int or float, etc
thanks for your answer it helped me alot . can you tell me how can i access specific elements in a table from lot of rows . by using . SELECT X_colm FROM MY_TABLE WHERE ID= 2; SELECT Y_colm FROM MY_TABLE WHERE ID=6; i want some thing like this ... You USed while loop it will continue in series .can i access specific row with this .
thanks alot i figered it out GUYS $result = mysql_query("SELECT x,y,z FROM MathTable WHERE id =4"); $result2 = mysql_query("SELECT x,y,z FROM MathTable WHERE id=56"); $row = mysql_fetch_array($result); $row2 = mysql_fetch_array($result2); $xAnswer = Add($row["x"],$row2["y"]); $yAnswer = Multiply($row2["z"],$row["y"]); $zAnswer = Subtract($row2["z"],$yAnswer); should i use it
0

mysql_connect is going to be deprecate. http://php.net/manual/en/function.mysql-connect.php

use mysqli instead http://www.php.net/manual/en/book.mysqli.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.