|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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"; |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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.. } |
|
![]() |
| Outils de la discussion | |
|
|