PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > function in mysql query
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
function in mysql query

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 14h03   #1
steeve_dun@SoftHome.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 07/11/2007, 14h16   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function in mysql query

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
==================

  Réponse avec citation
Vieux 07/11/2007, 20h58   #3
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function in mysql query

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

  Réponse avec citation
Vieux 07/11/2007, 23h54   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function in mysql query

On Wed, 07 Nov 2007 20:58:31 +0100, Darko <darko.maksimovic@gmail.com>
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 = "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,'bis') WHERE val2 IS NULL';

Allthough the very query screams out there's an error in database design..
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09671 seconds with 12 queries