473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=myuserf unction(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 2037
*** markus wrote/escribió (1 Jul 2004 01:38:45 -0700):
This string does not work in php:

$sql="UPDATE mytable SET myfield=myuserf unction(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.co m (markus) wrote:
This string does not work in php:

$sql="UPDATE mytable SET myfield=myuserf unction(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($val ue1){

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.co m (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($val ue1){

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="UPDAT E mytable SET myfield=myuserf unction(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.co m (markus) wrote in message
news:<b2******* *************** ****@posting.go ogle.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.co m (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
5478
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, produce a "function not defined" message when the SELECT statement is executed, even having previously worked on that machine. What can I do to correct this difficulty when it occurs? What do I need to know about user-defined functions in SELECT...
2
1393
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 to provide an interface for the user to add fields and possibly tables on the fly, with automated form and report modification and generation. I have never done this. While I have worked on projects where this might have been useful, it has...
5
2063
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 second user in succession I get permission denied when I try to select from a table that the user does have access to. Following is a cut and paste of two sessions showing what I mean (with certain details masked out). defender# psql db1-U...
8
1899
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 you have a page that takes an id number as a query string and displays different things based on that id number. If you were able to loop through running the aspx pages with id=100, id=200,
13
2530
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 report with a multiple-value discrete value and a rangevalue. The report shows fine in the viewer, but when I hit the export to pdf
5
2334
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 company there are also multiple contacts, some of which may be congruent. It's a very simple app. with only two forms. The first form shows two lists. The left-hand list is a Single-Select showing all the companies. The user chooses one company -...
18
23776
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 code/examples? Thanks in advance, Arthur
5
3631
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 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
5
9120
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 $conn = @mysql_connect($db_server,$db_user,$db_pass) or die("Connection to Database Server Failed"); @mysql_select_db($db) or die("Database Selection Failed"); return $conn; }
0
8063
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8002
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8496
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8148
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.