467,888 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

mySQL query in PHP - Parse error

JT
I am working on a basic webpage in PHP connecting to a mySQL database,
here is my query...

//query 1
$query_rs_staff = "SELECT * FROM jtStaff WHERE
locationID=".$_GET['myDesk'];
$rs_staff = mysql_query($query_rs_staff);
$row_rs_staff = mysql_fetch_assoc($rs_staff);
//query 2
$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff['sectionID']";
$rs_section = mysql_query($query_rs_section);
$row_rs_ssection = mysql_fetch_assoc($rs_section);
?>
I had it working with query 1. then I added query 2 to get from another

table, some additional data. So i have no problems with query 1. When
ran thru my server, it throws back a parse error on line 20, which is
the first line of query 2. can someone tell me the correct syntax for
what i am trying to do here?
thanks

Feb 3 '06 #1
  • viewed: 1541
Share:
9 Replies
Try

$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=" . $row_rs_staff['sectionID'];

like you did with the first query. I believe there are rules
concerning array references inside double quoted strings

Feb 3 '06 #2
JT wrote:
I am working on a basic webpage in PHP connecting to a mySQL database,
here is my query...

//query 1
$query_rs_staff = "SELECT * FROM jtStaff WHERE
locationID=".$_GET['myDesk'];
$rs_staff = mysql_query($query_rs_staff);
$row_rs_staff = mysql_fetch_assoc($rs_staff);
//query 2
$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff['sectionID']";
$rs_section = mysql_query($query_rs_section);
$row_rs_ssection = mysql_fetch_assoc($rs_section);
?>
I had it working with query 1. then I added query 2 to get from another

table, some additional data. So i have no problems with query 1. When
ran thru my server, it throws back a parse error on line 20, which is
the first line of query 2. can someone tell me the correct syntax for
what i am trying to do here?
thanks


Watch the quotes:

$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=". $row_rs_staff['sectionID'];

Personally, I like to have more structure to my queries:

$query_rs_section = "SELECT * "
."FROM jtSection "
."WHERE sectionID = ".$row_rs_staff['sectionID'];

-david-

Feb 3 '06 #3
JT wrote:
I am working on a basic webpage in PHP connecting to a mySQL database,
here is my query...

//query 1
$query_rs_staff = "SELECT * FROM jtStaff WHERE
locationID=".$_GET['myDesk'];
$rs_staff = mysql_query($query_rs_staff);
$row_rs_staff = mysql_fetch_assoc($rs_staff);

Looks fine.

//query 2
$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff['sectionID']";
Looks NOT fine.
Debugging is really easy with SQL.
Just place an echo here, like:

echo $query_rs_section;
exit;

and see what you make of it.
I am sure you'll fix it right away.
:-)
$rs_section = mysql_query($query_rs_section);
$row_rs_ssection = mysql_fetch_assoc($rs_section);
?>
I had it working with query 1. then I added query 2 to get from another

table, some additional data. So i have no problems with query 1. When
ran thru my server, it throws back a parse error on line 20, which is
the first line of query 2. can someone tell me the correct syntax for
what i am trying to do here?
In general: When hitting on SQL-(syntax)problems:
1) Echo the SQL before you execute it, and exit right there.
You have the query now on your webpage.

2) Look at it. If you do not see a problem, copy it and feed it directly to
the database via terminal, or whatever way you use to talk to your database
directly.

Good luck,
Erwin Moller


thanks


Feb 3 '06 #4
JT wrote:
I am working on a basic webpage in PHP connecting to a mySQL database,
here is my query...

//query 1
$query_rs_staff = "SELECT * FROM jtStaff WHERE
locationID=".$_GET['myDesk'];
Have you thoroughly sanitized $_GET['myDesk']?
Someone could tweak it to "1 or 42=42" giving strange results for the
following query.
$rs_staff = mysql_query($query_rs_staff);
$row_rs_staff = mysql_fetch_assoc($rs_staff);
//query 2
$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff['sectionID']";
$rs_section = mysql_query($query_rs_section);
$row_rs_ssection = mysql_fetch_assoc($rs_section);
?>
I had it working with query 1. then I added query 2 to get from another
table, some additional data. So i have no problems with query 1. When
ran thru my server, it throws back a parse error on line 20, which is
the first line of query 2. can someone tell me the correct syntax for
what i am trying to do here?


$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID={$row_rs_staff['sectionID']}";
### ______^__________________________^_

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 3 '06 #5
You need to select the DB and tables with in it at the top of your page
then you can get what ever you need out of those tables with simple sql
querys

Feb 3 '06 #6
ub******@gmail.com wrote:
You need to select the DB and tables with in it at the top of your page
then you can get what ever you need out of those tables with simple sql
querys

???
That sounds like strange advise to me, but maybe I misunderstand what you
say.
Please explain.

I always thought I name the tables I use in my query itself.
Not at top of the page...

And besides that: His SQL-string was wrong, because of a mistake.

Regards,
Erwin Moller
Feb 3 '06 #7
JT wrote:
I am working on a basic webpage in PHP connecting to a mySQL database,
here is my query...

//query 1
$query_rs_staff = "SELECT * FROM jtStaff WHERE
locationID=".$_GET['myDesk'];
$rs_staff = mysql_query($query_rs_staff);
$row_rs_staff = mysql_fetch_assoc($rs_staff);
//query 2
$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff['sectionID']";
$rs_section = mysql_query($query_rs_section);
$row_rs_ssection = mysql_fetch_assoc($rs_section);
?>
I had it working with query 1. then I added query 2 to get from another

table, some additional data. So i have no problems with query 1. When
ran thru my server, it throws back a parse error on line 20, which is
the first line of query 2. can someone tell me the correct syntax for
what i am trying to do here?
thanks

why not do this in one query.. It saves coding and reduces
network/system traffic by only returning columns and rows that are
necessary instead of "give me everything and let the program sort it
out". That method is considered VERY bad coding practices, but taught
in way too many institutions of "higher lerarning".

(you will need to fix the php exact syntax for this...)

select a.field1,a.field2,,,,b.field1,b.field2,,,,
from jtstaff a, jtsection b
where a.locationID= $_GET['myDesk']
and b.sectionid=a.sectionid

this will return all rows where locationid = mydesk and the
jsstaff.sectionid is in jtsection.stationid

depending upon what you are actually trying to retreive, you may also
need a locationid column (field) in the jtsection table to make them
more unique - then just add:

and a.locationid=b.locationid

to the end of the sql statement above.
Michael Austin
DBA.
Feb 4 '06 #8
JT
Thanks to all for the advice. Have taken it all on board and made the
suggested improvements to my code. Its all working now. Many thanks.

Feb 6 '06 #9

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...
JT wrote:
I am working on a basic webpage in PHP connecting to a mySQL database,
here is my query...

//query 1
$query_rs_staff = "SELECT * FROM jtStaff WHERE
locationID=".$_GET['myDesk'];
Have you thoroughly sanitized $_GET['myDesk']?
Someone could tweak it to "1 or 42=42" giving strange results for the
following query.
$rs_staff = mysql_query($query_rs_staff);
$row_rs_staff = mysql_fetch_assoc($rs_staff);
//query 2
$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff['sectionID']";
$rs_section = mysql_query($query_rs_section);
$row_rs_ssection = mysql_fetch_assoc($rs_section);
?>
I had it working with query 1. then I added query 2 to get from another
table, some additional data. So i have no problems with query 1. When
ran thru my server, it throws back a parse error on line 20, which is
the first line of query 2. can someone tell me the correct syntax for
what i am trying to do here?


or you can do it like this:

$query_rs_section = "SELECT * FROM jtSection WHERE
sectionID=$row_rs_staff[sectionID]";

whatever suits your fancy.
### ______^__________________________^_

--
If you're posting through Google read <http://cfaj.freeshell.org/google>

Feb 11 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by J Sharman | last post: by
9 posts views Thread by SuSe newbie | last post: by
3 posts views Thread by Bert Sierra | last post: by
11 posts views Thread by Howard | last post: by
1 post views Thread by jimmy | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.