call database row in to a function - php

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:
http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx
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

Related

How to return MySQL records via PHP function call using PDO and Prepared Statements?

I am trying to find the best way to get records from a MySQL table via a PHP function call using a prepared statement and PDO?
Efficiency is not the issue (I am sure there is perhaps a faster way or more memory efficient way to accomplish this task). My goal is to make the code simple, with one call to a function for to obtain records via a PHP function call and a using PDO prepared statement.
The caller so far looks like this:
$records = DB_Get_Records($pdo_connection, '"SELECT * FROM Table1 WHERE ID = ?', array($record_id);`
And the function looks like this:
function DB_Get_Records(&$pdo_connection, &$sql_statement, &$records_located, &$select_string, &$param_array) {
$sql_statement = $pdo_connection->prepare($select_string);
$sql_statement->execute($param_array);
$records_located = $sql_statement->fetch(PDO::FETCH_ASSOC);
return $records_located;
Because of the scope of the params, I am using pass by reference. But I am not sure how to best define in the main portion of the script. If I use pass by value, I believe that all results will be lost after returning from the function.
The confusion is how to define them prior to the function call so that they will receive the data. I am not sure if I have to reserve space of a some sort prior to calling the function. When using
$records_located = $sql_statement->fetch(PDO::FETCH_ASSOC);
In either the main portion of the script or the function, $records_located receives the records.
But trying to path this information from the caller to the main is not clear to me. When used in the function, the fetch statement (I believe) will create the necessary space using $records_located as the pointer. The question that pops up seems to be is how? Is the receiving variable used to create an array and then setting a point to $records_located variable? If so, then the scope would of the array would likely be limited to the function call. And if that is the case, then -- although the data pointer is retained -- the actual data returned from fetch would be gone after the function returns.
Hopefully, I am missing something here or overthinking the issue.
Again, it is not about trying to maximize speed of memory usage. It is simply about making simple calls to the database of one line instead of many lines to setup each call to get records.
Thanks for any help you can provide!
Looks like you need to forget all about allocating memory and who does what where, PHP mostly does that for you. If you change all of your parameters to NOT be passed by reference and then
return $sql_statement->fetchAll(PDO::FETCH_ASSOC);
This will just return an array of the results and no scope is involved

PHP function on SQL selectee

Is it possible to perform a specific PHP function on data that is being returned by a database query, at the very moment this query is still running?
Let's say we are in some class and we have the following pseudo SQL which should return a couple of rows:
$results = $this->db->query("SELECT PHP_function(column), column 2 FROM table")->fetchAll(PDO::FETCH_ASSOC);
PHP_function could be json_decode for example.
Or is this not possible and would it require an additional loop on the results in order to do such a thing?
A manual page for the very function you're using contains an example
No, this is not possible. SQL engine (mysql or any other) is a separate application. PHP can communicate with it, send queries and receive data. Depending on what you want to do in that custom function, you may be able to write a custom function in mysql to do the same thing.
Have a look at create function documentation for info on how to do that.
Where's the problem with this?
<?php
foreach($results as $key=>$res){
$results[$key]['column'] = PHP_function($res['column']);
}
Beside that MySQL has some useful functions included already, so maybe that one you need is usable right in the query
As i know it's not possible to achieve php manipulation while query running , you need to take result and then loop aur use predefined function from

PHP form code maintainance

I am working on a database in mysql and I need to make a user facing page that allows me to enter text for each field and then submit the record. I have been able to accomplish this easily, however it is getting quit annoying having to update the input.html and save.php every time I decide to add/remove a field.
It really seems like there should be some sort of program that can auto-maintain the code for me and allow me to just focus on the database structure. Does anyone know of something that does this? I feel like I am doing it all wrong.
Thanks in advance.
P.S. I realize that I could just use phpmyadmin, but I do not want to give full DB access to my data entry people; plus they are not technical types, I don't want to intimidate them.
In the end, I decided to make my own script, using INFORMATION_SCHEMA to get the field names, and then made a recursive loop to add each record. It wasn't hard, but it seems like there should still be a better way.

PHP Update class (OO)

I have written a PHP OO class which will update 4 fields of a certain row in a table. For now the row is decided by a constant (user with name 'jip')
I have corrected the query in a previous post here, so i'm pretty sure the query itself is fine. So, there must be some sort of error within the class itself. Probaply the vars don't reach the query somehow. I have been looking for hours, but can't find the problem. I have linked both files of the class, since i downt know where the error is, the values just don't show up in the database. If anyone would like to check them, (s)he'd make my entire week! SO here is the link and i hope someone is willing to help :)
UpdateForm.php: http://pastebin.com/dUaZPrn6
Update.class.php: http://pastebin.com/6mnL4DzE
Try replacing mysqli_real_escape_string($conn, $variable) with
$conn->real_escape_string($variable);
For example,
$conn->real_escape_string($this->Lengte_update);
You're using the object-oriented style, so you can't use the procedural escape function. See the docs on mysqli::real_escape_string.
Edit:
The query isn't being executed. You assign the query to $query, but you need to call
$conn->query($query);
For anything to happen in the database.

PHP next MySQL row - how to move pointer until function checks true?

I have a PHP script which takes a value from a row in my MySQL database, runs it through a function, and if it determines it's true returns one value, and if it's false, it needs to go to the next value in the database and check that one until eventually one returns true.
I think I need to use mysql_fetch_assoc, but I'm not really sure in what way to use it... I wish I could post my code to be more specific, but it's a lot of code and most of it has no bearing on this issue...
Is the "function" something you could do in the database instead? It's really inefficient to process every row in the table to check for some type of condition. That's exactly what databases are good at, namely, processing queries efficiently and getting answers to you quickly.
So I'd recommend looking at how to do it all on the database side so that your PHP code is just fetching the end result (i.e. rows filtered by the function). Maybe if you provide more details of what your "function" is doing, a more specific answer can be provided.
You can use mysql_fetch_array, and just jump on the values fetched using $row[id] and not $row['name'].
Say your function returns true, you'd just use $row[lastid+1].
If the ID isn`t incremental, this could work :
$qry_result = mysql_query($qry) or die(mysql_error());
while ($row = mysql_fetch_array($qry_result)) {
$result = yourfunction($row['whatever');
if ($result != false)
break;
}
Also there is a php function next() which advances a pointer to the next array element. In your function you could implement an array builder, and then cycle between the elements with this. Depends on what your function actually does or script purpose is. It could lead to some load if there are alot of results.
You should not check database this way, as mentioned above. Database has a little difference from the plain text file.
You should not have a field in your database that has a bunch of values separated by value1:value2|value1:value2|value1:value2. It must be separate fields. Database has a little difference from the plain text file and you better learn it.
You could try something like this:
SELECT *
FROM table
WHERE field NOT LIKE '%$sessionvar:%';
I think that is what you are after?

Categories