Tutorial :call database row in to a function


I am writing a function which is called into a page, but I am not sure how to call information form database into the function in order to use them. Basically I am doing some calculation in the function where I need information form database to do them.

Is there anyone who can give a clue on how ot do this? Many thanks F

From a comment...

What I am trying to do is this: I have a page in php which retieves some info from database and all works fine. I am writing a function that needs to make some calculation based on some fields in the database. What I cannot solve is how to get this information form the database into my function. I have tired this: function CalculateCost () { $low_season = $row_rsbooking['cost']; etc. etc. then making some calculations but I am getting nowhere. I am not sure if the function is getting the information form database in order to make calculation.


This is an older question, but as it remains unanswered, I will proffer my two cents.

From your comment to Rafael, it looks like your problem is variable scope. I think you want something like this

function add_one($cost)   {     // just something irrelevant to do    return $cost + 1;  }    // query stuff here, leaving $row with the results  $new_value = add_one($row_rsbooking['cost']);  

In other words, pass the column value you need to the function and process the results it returns. If you need to alter your actual row, you could pass the whole row by reference (i.e. add_one(&$row)) and modify it in your function.

Without some more code from you, this is my best guess.


I am not sure I understand the question. But will try to answer it the best way I can.

If you are looking for help on how to connect to a database and execute a query, then have a look at the following link:


It describes how you can use the SQL Command object to get data from a SQL Server database in the form of a Dataset. Once this is done, you can get a dataset or datatable to get data to any function in your code for calculations.

You could also use an odbccommand. I would post a link, but I don't have enough reputation points to do more than one! :o)

If the Dataset is too big, you could consider using a datable or a SQLDataReader. Again, please have a look at the MSDN for info on those classes.

I hope this helps!

Rafael Jovel www.augensoftwaregroup.com

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »