dbTalk Databases Forums  

Syntax for Query

comp.databases.mysql comp.databases.mysql


Discuss Syntax for Query in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
GarryJones
 
Posts: n/a

Default Syntax for Query - 01-12-2011 , 10:12 AM






When creating a query to select data from the Mysql database via
phpadmin this works...

-----------------------------
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ') && bok_adminkod < 2) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------

However I'd like to make the 2 a variable. So I set $myvar to 2 as
follows....

I tried
-----------------------------
$myvar = 2;
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ') && bok_adminkod < $myvar) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------
But it does not find any records. What am I missing in the syntax?

Any help greatly appreciated.

Garry Jones
Sweden

Reply With Quote
  #2  
Old   
fabrice régnier
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 10:30 AM






hi,

what if you echo the $paigntontb var ?
i think it should work.

what if you write instead:

$myvar = 2;
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ')&& bok_adminkod< '. $myvar.') ORDER BY
bokeft,bokfor,bokanmref';

regards,

f.


Le 12/01/2011 17:12, GarryJones a écrit :
Quote:
When creating a query to select data from the Mysql database via
phpadmin this works...

-----------------------------
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ')&& bok_adminkod< 2) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------

However I'd like to make the 2 a variable. So I set $myvar to 2 as
follows....

I tried
-----------------------------
$myvar = 2;
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ')&& bok_adminkod< $myvar) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------
But it does not find any records. What am I missing in the syntax?

Any help greatly appreciated.

Garry Jones
Sweden

Reply With Quote
  #3  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 10:31 AM



GarryJones wrote:
Quote:
When creating a query to select data from the Mysql database via
phpadmin this works...

-----------------------------
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ') && bok_adminkod < 2) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------

However I'd like to make the 2 a variable. So I set $myvar to 2 as
follows....

I tried
-----------------------------
$myvar = 2;
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ') && bok_adminkod < $myvar) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------
But it does not find any records. What am I missing in the syntax?

a lot.

once inside the single tick, you are in string mode.

I am surprised the original query worked.

Use strings like this:

$query="select A from b where id='".$c."'"
$result=mysql_query($query);

since SQL likes single ticks, use double to demarcate the string or PHP.

Even better is sprintf

i.e. $query="select a from b where id='%d'",$c);

Sine %d will try and make $c an integer, even if it is a hackers string
or whatever, it won't do too much damage.

Reply With Quote
  #4  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 10:46 AM



On 1/12/2011 11:12 AM, GarryJones wrote:
Quote:
When creating a query to select data from the Mysql database via
phpadmin this works...

-----------------------------
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ')&& bok_adminkod< 2) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------

However I'd like to make the 2 a variable. So I set $myvar to 2 as
follows....

I tried
-----------------------------
$myvar = 2;
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ')&& bok_adminkod< $myvar) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------
But it does not find any records. What am I missing in the syntax?

Any help greatly appreciated.

Garry Jones
Sweden

Is it not finding any rows, or do you have an error in your SQL? When
you issue the mysql_query(), do you get back a result set (even if it is
empty), or do you get false? If the latter, your query is bad-
mysql_error() will help you find the problem.

If you are getting back an empty result set, then your data isn't like
you think it is. Like fabrice suggested, echo the query before
executing it and see if it contains what you think it does. Chances are
you'll see your problem. If not, echo the original query and see what
it shows.

BTW - it will be much easier to read your query if you format it better,
i.e.

$paigntontb = 'SELECT * ' .
'FROM ct_bok ' .
'LEFT JOIN resmal ON (resmal.resmalid=ct_bok.resmalvalid) ' .
'LEFT JOIN buss_plats ON (buss_plats.buss_req = ct_bok.bokbus) ' .
'WHERE (resmalvalid IN (implode(', ', $items) . ') ' .
&& bok_adminkod < $myvar) ' .
'ORDER BY bokeft,bokfor,bokanmref';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #5  
Old   
Helmut Chang
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 12:03 PM



Am 12.01.2011 17:31, schrieb The Natural Philosopher:

Quote:
i.e. $query="select a from b where id='%d'",$c);

Sine %d will try and make $c an integer, even if it is a hackers string
or whatever, it won't do too much damage.
And since it's an integer, why would you want to enclose it with ticks
in the query?

Helmut

Reply With Quote
  #6  
Old   
GarryJones
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 01:57 PM



Thanks for your help everyone.

This

bok_adminkod< '. $myvar .'

did it...

Reply With Quote
  #7  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 06:46 PM



GarryJones wrote:
Quote:
Thanks for your help everyone.

This

bok_adminkod< '. $myvar .'

did it...

Reply With Quote
  #8  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 06:46 PM



Helmut Chang wrote:
Quote:
Am 12.01.2011 17:31, schrieb The Natural Philosopher:

i.e. $query="select a from b where id='%d'",$c);

Sine %d will try and make $c an integer, even if it is a hackers string
or whatever, it won't do too much damage.

And since it's an integer, why would you want to enclose it with ticks
in the query?

Helmut
habit ;-)

Reply With Quote
  #9  
Old   
Peter H. Coffin
 
Posts: n/a

Default Re: Syntax for Query - 01-12-2011 , 06:52 PM



On Wed, 12 Jan 2011 08:12:38 -0800 (PST), GarryJones wrote:
Quote:
When creating a query to select data from the Mysql database via
phpadmin this works...

-----------------------------
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ') && bok_adminkod < 2) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------

However I'd like to make the 2 a variable. So I set $myvar to 2 as
follows....

I tried
-----------------------------
$myvar = 2;
$paigntontb = 'SELECT * FROM ct_bok LEFT JOIN resmal ON
(resmal.resmalid=ct_bok.resmalvalid) LEFT JOIN buss_plats ON
(buss_plats.buss_req = ct_bok.bokbus) WHERE (resmalvalid IN (' .
implode(', ', $items) . ') && bok_adminkod < $myvar) ORDER BY
bokeft,bokfor,bokanmref';
$paigntontbd=mysql_query($paigntontb);
-----------------------------
But it does not find any records. What am I missing in the syntax?
Nothing. But you are missing how your particular application language
handles variable interpretation in single-quoted strings.

My guess is that you'll want to review
http://php.net/manual/en/language.types.string.php

--
59. I will never build a sentient computer smarter than I am.
--Peter Anspach's list of things to do as an Evil Overlord

Reply With Quote
  #10  
Old   
strawberry
 
Posts: n/a

Default Re: Syntax for Query - 01-14-2011 , 04:16 AM



I agree about formatting, but isn't this easier still...
I've centrally justified it too but I don't think this forum supports BBCode.

$items = implode(', ', $items);
$paigntontb = "
SELECT *
FROM ct_bok cb
LEFT
JOIN resmal r
ON r.resmalid=cb.resmalvalid
LEFT
JOIN buss_plats bp
ON bp.buss_req = cb.bokbus
WHERE cb.resmalvalid IN ($items)
AND cb.bok_adminkod < $myvar
ORDER
BY bokeft
, bokfor
, bokanmref"

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.