473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Is it possible to use php user function in a single global SQL query?

This string does not work in php:

$sql="UPDATE mytable SET myfield=myuserfunction(myfield)"

What I want is my function to evaluate each field and put the new
value in the field.
For example, this works:
$sql= "UPDATE mytable SET myfield=myfield+3";

Stores myfield+3 in each myfield, but with a function it does not
work.

Obviously, multiple queries in a loop do work but what I need is a
single call.

Thanks
Jul 17 '05 #1
8 2023
*** markus wrote/escribió (1 Jul 2004 01:38:45 -0700):
This string does not work in php:

$sql="UPDATE mytable SET myfield=myuserfunction(myfield)"
You can't put PHP code inside a string, it that's what you mean.
What I want is my function to evaluate each field and put the new
value in the field.
For example, this works:
$sql= "UPDATE mytable SET myfield=myfield+3";

Stores myfield+3 in each myfield, but with a function it does not
work.

Obviously, multiple queries in a loop do work but what I need is a
single call.


So you want to get a value from a database, pass it to a PHP function and
then use the returned value in a database query, all in one single call; is
that correct? Sorry: you can't.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #2
On 1 Jul 2004 01:38:45 -0700, m7***@hotpop.com (markus) wrote:
This string does not work in php:

$sql="UPDATE mytable SET myfield=myuserfunction(myfield)"

What I want is my function to evaluate each field and put the new
value in the field.

For example, this works:
$sql= "UPDATE mytable SET myfield=myfield+3";

Stores myfield+3 in each myfield, but with a function it does not
work.


My office SQL guru tells me that MS SQL Server has such a thing as
User Defined Functions, which *may* be evaluated per row. He has never
had a need to used UDFs.

However, if you're using MySQL, you'd need to execute multiple UPDATE
statements. That said, depending on what your function does, it may be
possible to write it in SQL.

I don't know about any other RDBMSs.

--
David ( @priz.co.uk ). <http://www.priz.co.uk/ipdb/>
Build the Village: <http://www.priz.co.uk/build/>
Why which is what or whoever.
Jul 17 '05 #3
$sql= "UPDATE mytable SET myfield=myfield+3";

My function simply evaluates a field against numeric values and and returns a value

function myfunction($value1){

if (value1>20 && value1 <=200) {
new=100;

}else if(value1>200 && value1 <300)){
new=523;

}else{
new=2;
}

return new;
} //end function
Jul 17 '05 #4
On 1 Jul 2004 12:19:39 -0700, m7***@hotpop.com (markus) wrote:
$sql= "UPDATE mytable SET myfield=myfield+3";

My function simply evaluates a field against numeric values and and returns a value

function myfunction($value1){

if (value1>20 && value1 <=200) {
new=100;

}else if(value1>200 && value1 <300)){
new=523;

}else{
new=2;
}

return new;
} //end function


You can do that with a CASE expression in SQL.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5
>>$sql="UPDATE mytable SET myfield=myuserfunction(myfield)"

What I want is my function to evaluate each field and put the new
value in the field.

For example, this works:
$sql= "UPDATE mytable SET myfield=myfield+3";

Stores myfield+3 in each myfield, but with a function it does not
work.


My office SQL guru tells me that MS SQL Server has such a thing as
User Defined Functions, which *may* be evaluated per row. He has never
had a need to used UDFs.


MySQL also permits User Defined Functions. But you need to compile
them into a plugin and register them with MySQL, something that
seems annoying enough that you shouldn't do it lightly. If you've
got some generally useful functions that you could use a lot, they
might be worth doing. You also have to deal with the issue that
the functions won't be on every MySQL server, and chances are hosting
companies won't let you add your own plugins to MySQL unless you
lease the entire server.

One good candidate for a UDF that I've seen discussed here is one
that calculates a distance between two pairs of lat/long coordinates,
so that you can, for example, select the nearest 5 stores (with
known coordinates) to the center of the Zip code (yes, this is
USA-centric) the user supplied (tables of zip code to lat/long are
apparently available somewhere. The US Census TIGER data could
give you lat/long coordinates of a street block but you'd need a
complicated matching algorithm to match user input against the
addresses in that data. I think you could also use that data to
compute a center-of-gravity for a Zip code (5 or 9 digits, but it
likely doesn't include Zip codes that exist only inside a Post
Office for the PO Boxes.) You'd definitely want to pre-crunch this
data rather than doing it on each query.).

Your best bet is to translate the PHP function into a SQL function,
if possible.

Gordon L. Burditt
Jul 17 '05 #6
This is just a casual function. The fact that I have to register MySQL
user functions with the server makes them useless in practice for the
average developer.

My best bet is therefore perform multiple queries in a loop which was
my initial code. I wonder if this is too much stress on the database
with 20000 records, though.
Jul 17 '05 #7
m7***@hotpop.com (markus) wrote in message
news:<b2**************************@posting.google. com>...

What I want is my function to evaluate each field and put the new
value in the field.
A user-defined function (UDF) is a way to extend MySQL with
a new function that works like native (built in) MySQL function
such as ABS() and CONCAT().

...

For instructions on writing user-defined functions, see section
23.2 Adding New Functions to MySQL. For the UDF mechanism to work,
functions must be written in C or C++, your operating system must
support dynamic loading and you must have compiled mysqld
dynamically (not statically).

http://dev.mysql.com/doc/mysql/en/CREATE_FUNCTION.html

In other words, if you want write user-defined functions in SQL,
consider switching to PostgreSQL or a commercial SQL engine.
For example, this works:
$sql= "UPDATE mytable SET myfield=myfield+3";

Stores myfield+3 in each myfield, but with a function it does not
work.

Obviously, multiple queries in a loop do work but what I need is a
single call.


Consider writing a query that will work along the lines of

UPDATE mytable SET myfield=myfield+3

Cheers,
NC
Jul 17 '05 #8
On 2 Jul 2004 09:33:56 -0700, m7***@hotpop.com (markus) wrote:
This is just a casual function. The fact that I have to register MySQL
user functions with the server makes them useless in practice for the
average developer.

My best bet is therefore perform multiple queries in a loop which was
my initial code. I wonder if this is too much stress on the database
with 20000 records, though.


If your function performs what you posted previously in this thread, your best
bet is just writing a CASE statement that does the same thing.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
2
by: brucedodds | last post by:
This post is for senior developers. I'm an Access contractor. This morning I see a listing for a position that seems to require development of a self-modifying database. The application may need...
5
by: Chris Ochs | last post by:
It doesn't currently seem possible to switch between different users using SET SESSION AUTHORIZATION. If I log in as the superuser and switch to another user that works, but if I then switch to a...
8
by: Daniel | last post by:
Hi, Does anyone know if it is possible to put an aspx page inside of another? OR run an aspx page and capture the output as a string and then write this out to a page.... So for example say...
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
5
by: teddysnips | last post by:
I have to write an application to do some data cleansing. It's a Contact database, but over a number of years there are multiple companies which are all essentially the same entity. For each...
18
by: Arthur | last post by:
Hi All, I would like to get the name of the user given their networkID, is this something Active Directory would be useful for?(For intranet users) If so, can you please point me to some sample...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.