function in mysql query

function in mysql query

am 07.11.2007 14:03:41 von steeve_dun

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

Re: function in mysql query

am 07.11.2007 14:16:20 von Jerry Stuckle

steeve_dun@SoftHome.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.
jstucklex@attglobal.net
==================

Re: function in mysql query

am 07.11.2007 20:58:31 von darko

On Nov 7, 2:03 pm, steeve_...@SoftHome.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

Re: function in mysql query

am 07.11.2007 23:54:08 von luiheidsgoeroe

On Wed, 07 Nov 2007 20:58:31 +0100, Darko =

wrote:

> On Nov 7, 2:03 pm, steeve_...@SoftHome.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 =3D "UPDATE vals SET val2 =3D '".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 =3D "UPDATE vals SET val2 =3D '" . func1("val1") . "' WHERE val=
2 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 =3D 'UPDATE vals SET val2 =3D CONCAT(val1,'bis') WHERE val2 IS NU=
LL';

Allthough the very query screams out there's an error in database design=
..
-- =

Rik Wasmus