473,813 Members | 3,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function in mysql query

Hi,
What I want is to check for empty values for field 2 and put a result
a function instead.
eg :

function func1($val){
return $val.'bis';
}

$query = "UPDATE vals SET val2 = '".func1(`val1` )."' WHERE `val2` IS
NULL";

but the problem is that I can't recover val1 in func1

So, is there a way to do so?
Thanks

Nov 7 '07 #1
3 1665
st********@Soft Home.net wrote:
Hi,
What I want is to check for empty values for field 2 and put a result
a function instead.
eg :

function func1($val){
return $val.'bis';
}

$query = "UPDATE vals SET val2 = '".func1(`val1` )."' WHERE `val2` IS
NULL";

but the problem is that I can't recover val1 in func1

So, is there a way to do so?
Thanks

Nope, you can't retrieve the value in an update statement. You'll need
to SELECT the row then process it.

Or check a SQL newsgroup for a way to do it in SQL.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Nov 7 '07 #2
On Nov 7, 2:03 pm, steeve_...@Soft Home.net wrote:
Hi,
What I want is to check for empty values for field 2 and put a result
a function instead.
eg :

function func1($val){
return $val.'bis';

}

$query = "UPDATE vals SET val2 = '".func1(`val1` )."' WHERE `val2` IS
NULL";

but the problem is that I can't recover val1 in func1

So, is there a way to do so?
Thanks
What do you mean by "can't *recover* vall *in funcl*"?
The first thing that I notice is you use those weirh ` simbols instead
of ' or "". And furhter, "WHERE `val2` IS NULL" also confuses me, why
do you need ` around val2?

$query = "UPDATE vals SET val2 = '" . func1("val1") . "' WHERE val2 IS
NULL";

Maybe I'm missing the point?

Darko

Nov 7 '07 #3
On Wed, 07 Nov 2007 20:58:31 +0100, Darko <da************ **@gmail.com
wrote:
On Nov 7, 2:03 pm, steeve_...@Soft Home.net wrote:
>Hi,
What I want is to check for empty values for field 2 and put a result
a function instead.
eg :

function func1($val){
return $val.'bis';

}

$query = "UPDATE vals SET val2 = '".func1(`val1` )."' WHERE `val2`IS
NULL";

but the problem is that I can't recover val1 in func1

So, is there a way to do so?
Thanks

What do you mean by "can't *recover* vall *in funcl*"?
The first thing that I notice is you use those weirh ` simbols instead
of ' or "". And furhter, "WHERE `val2` IS NULL" also confuses me, why
do you need ` around val2?
Standard mysql quoting of database/table/fieldnames (which means you can
use reserved names, normally they are not necessary), allthough the OP
didn't realize that:
- `` means something totally different in PHP
- MySQL cannot use PHP functions during queries
- what string concatination actually is
$query = "UPDATE vals SET val2 = '" . func1("val1") . "' WHERE val2 IS
NULL";

Maybe I'm missing the point?
The OP should either use function available in his database, or define a
function in his database if that's more to his liking and supported.

In case of mysql, one might do this:

$query = 'UPDATE vals SET val2 = CONCAT(val1,'bi s') WHERE val2 IS NULL';

Allthough the very query screams out there's an error in database design..
--
Rik Wasmus
Nov 7 '07 #4

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

Similar topics

9
4968
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
2490
by: John Pastrovick | last post by:
I use a function, myrandomPIN (), to generate random PIN numbers. The following sql query updates records with the SAME PIN number but. I want to generate DIFFERENT pin numbers for every record. The function is ok but I can't figure out how to run it individually for each record. In other words I do not know the correct syntax to use UPDATE in a loop (if necessary) so that a different call to the function is done every time or ecah...
8
2047
by: markus | last post by:
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";
12
4787
by: Prabu Subroto | last post by:
Dear my friends... I am using postgres 7.4 and SuSE 9.1. I want to use auto_increment as on MySQL. I look up the documentation on www.postgres.com and I found "serial" . But I don't know how to create auto_increment. here is my try:
1
3384
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced no protests when built, ran and connected with no
3
13958
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE: The problem here is PHP not MySQL, in MySQL everything works just fine. Here is the query that I wrote for getting the number of how much
1
6507
by: Jaime Leivers | last post by:
Here's a median function in access that you can call in any query. You could change this to any excel function you wanted. Most people can find the windows help file that says how to call an excel function but don't know how to pass an array of the recordset they made into that function. This uses GetRows that nicely creates an array that can be passed into excel. I have this data table for testing. The table name is TestData ...
11
3829
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result = mysql_query("select * from $tableInfo") ; for ($i = 0; $i < mysql_num_rows($result); $i++) {
5
9146
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
9734
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
9607
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
10404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7678
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...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4357
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
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
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.