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.databases.mysql > Variable in Select statement?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Variable in Select statement?

Réponse
 
LinkBack Outils de la discussion
Vieux 01/10/2007, 09h50   #1 (permalink)
GarryJones
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Variable in Select statement?

I am writing a mysql select statement in php and I would like to
include a variable in it but cant grasp the syntax.

// Example

What I want to do

if ($forma == "mot" ) {
$fishnet = "(1,2,3,4)";
} else {
$fishnet = "(3,4,7)";
}

$ml_collect='SELECT * FROM ml_lopp WHERE ml_lopp.sank IN $fishnet
ORDER BY date1, ml_lopp.loppnum';

..... returns no rows. However the code itself with manually inserted
values...

$ml_collect='SELECT * FROM ml_lopp WHERE ml_lopp.sank IN (1,2,3,4)
ORDER BY date1, ml_lopp.loppnum';

.....and.....

$ml_collect='SELECT * FROM ml_lopp WHERE ml_lopp.sank IN (3,4,7) ORDER
BY date1, ml_lopp.loppnum';

.... both work correctly.

I simply want to set the value of $fishnet to define the where clause

Any greatly appreaciated

Garry Jones
Sweden

  Réponse avec citation
Vieux 01/10/2007, 09h52   #2 (permalink)
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

On Mon, 01 Oct 2007 10:50:53 +0200, GarryJones <morack@algonet.se> wrote:

> I am writing a mysql select statement in php and I would like to
> include a variable in it but cant grasp the syntax.
>
> // Example
>
> What I want to do
>
> if ($forma == "mot" ) {
> $fishnet = "(1,2,3,4)";
> } else {
> $fishnet = "(3,4,7)";
> }
>
> $ml_collect='SELECT * FROM ml_lopp WHERE ml_lopp.sank IN $fishnet
> ORDER BY date1, ml_lopp.loppnum';


Change the single to double quotes. If you'd have echo()ed your query
before sending it to MySQL you would have spotted the error instantly.
--
Rik Wasmus
  Réponse avec citation
Vieux 01/10/2007, 10h46   #3 (permalink)
GarryJones
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

Great, that worked.

The next problem is I now need to test for a value in the select
statment to see if it is in an array.

$ml_collect_ind="SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
((in_array(loppnum, $arr_motlopp))=TRUE) ORDER BY sank, loppnum";

This should return all fields where 'scfmnum' is $scfchknum and the
associated value 'loppnum' is in the array.

Needless to say its syntax again..

Any much appreciated

Garry Jones
Sweden

  Réponse avec citation
Vieux 01/10/2007, 10h49   #4 (permalink)
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

On 1 Oct, 10:46, GarryJones <mor...@algonet.se> wrote:
> Great, that worked.
>
> The next problem is I now need to test for a value in the select
> statment to see if it is in an array.
>
> $ml_collect_ind="SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
> ((in_array(loppnum, $arr_motlopp))=TRUE) ORDER BY sank, loppnum";
>
> This should return all fields where 'scfmnum' is $scfchknum and the
> associated value 'loppnum' is in the array.
>
> Needless to say its syntax again..
>
> Any much appreciated
>
> Garry Jones
> Sweden


$ml_collect_ind="SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
" .
((in_array(loppnum, $arr_motlopp)) . "=TRUE) ORDER BY sank, loppnum";

  Réponse avec citation
Vieux 01/10/2007, 11h13   #5 (permalink)
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

On Mon, 01 Oct 2007 11:49:26 +0200, Captain Paralytic
<paul_lautman@yahoo.com> wrote:

> On 1 Oct, 10:46, GarryJones <mor...@algonet.se> wrote:
>> Great, that worked.
>>
>> The next problem is I now need to test for a value in the select
>> statment to see if it is in an array.
>>
>> $ml_collect_ind="SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
>> ((in_array(loppnum, $arr_motlopp))=TRUE) ORDER BY sank, loppnum";
>>
>> This should return all fields where 'scfmnum' is $scfchknum and the
>> associated value 'loppnum' is in the array.
>>
>> Needless to say its syntax again..
>>
>> Any much appreciated
>>
>> Garry Jones
>> Sweden

>
> $ml_collect_ind="SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
> " .
> ((in_array(loppnum, $arr_motlopp)) . "=TRUE) ORDER BY sank, loppnum";


Huh?
$ml_collect_ind = "SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
loppnum IN (".implode(',',$arr_motlopp).") ORDER BY sank, loppnum";

--
Rik Wasmus
  Réponse avec citation
Vieux 01/10/2007, 11h59   #6 (permalink)
GarryJones
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

That looks okay, but I can not get it working...

The declaration

$ml_collect_ind='SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
" . ((in_array(loppnum, $arr_motlopp)) . " =TRUE) ORDER BY sank,
loppnum';

Gives me the following echo

SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND " .
((in_array(loppnum, $arr_motlopp)) . " =TRUE) ORDER BY sank, loppnum

It fails to find anything valid when the first "where" condition is
correct and the other value is in the array,

Is there something in the wrong place?

All appreciated

Garry Jones
Sweden





  Réponse avec citation
Vieux 01/10/2007, 12h33   #7 (permalink)
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

On 1 Oct, 11:59, GarryJones <mor...@algonet.se> wrote:
> That looks okay, but I can not get it working...
>
> The declaration
>
> $ml_collect_ind='SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
> " . ((in_array(loppnum, $arr_motlopp)) . " =TRUE) ORDER BY sank,
> loppnum';
>
> Gives me the following echo
>
> SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND " .
> ((in_array(loppnum, $arr_motlopp)) . " =TRUE) ORDER BY sank, loppnum
>
> It fails to find anything valid when the first "where" condition is
> correct and the other value is in the array,
>
> Is there something in the wrong place?
>
> All appreciated
>
> Garry Jones
> Sweden


You have been told once already to use double quotes. My suggestion
used double quotes. Your test used single quotes.

  Réponse avec citation
Vieux 02/10/2007, 14h02   #8 (permalink)
Aaron Saray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable in Select statement?

On Oct 1, 5:59 am, GarryJones <mor...@algonet.se> wrote:
> That looks okay, but I can not get it working...
>
> The declaration
>
> $ml_collect_ind='SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND
> " . ((in_array(loppnum, $arr_motlopp)) . " =TRUE) ORDER BY sank,
> loppnum';
>
> Gives me the following echo
>
> SELECT * FROM ml_lopp WHERE scfmnum=$scfchknum AND " .
> ((in_array(loppnum, $arr_motlopp)) . " =TRUE) ORDER BY sank, loppnum
>
> It fails to find anything valid when the first "where" condition is
> correct and the other value is in the array,
>
> Is there something in the wrong place?
>
> All appreciated
>
> Garry Jones
> Sweden


I think you may be making a wasteful sql call. Think of it this way:

You already know something that you're using to conditionally return
row results.

>From what it looks like, you're trying to see if something is in your

array using php... if it is, you want basically it to say "if true =
true". You figure maybe if its not in the array, you'll get "if false
= true". Instead, why don't you do:

if (in_array(.......... {
// do mysql call without the in_array now.
// possibly get the return results, set our variables, etc.
}
else {
// not found in array
// print error... or set our number of rows to zero if you're using
that for your code..
}



  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 00h59.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13182 seconds with 16 queries