?
Solved

Why isn't this query working?

Posted on 2012-12-21
8
Medium Priority
?
454 Views
Last Modified: 2012-12-21
I am converting a site from ColdFusion to PHP.

When I run this query:

$query = 'SELECT TOP e.date
                        ,e.startTime
                        ,e.endTime
                        ,e.title
                        ,e.details
                        ,l.name
                        ,e.room
                  FROM
                        WWW.dbo.Events e
                        LEFT OUTER JOIN WWW.dbo.Locations l
                              ON e.locationId = l.locationId
                  WHERE
                        WHERE e.date BETWEEN GETDATE() and DATEADD(month, 6, GETDATE())
                  ORDER BY
ORDER BY date ASC';

I get this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'e'.' in C:\inetpub\wwwroot\db2.php:67 Stack trace: #0 C:\inetpub\wwwroot\db2.php(67): PDO->query('SELECT TOP e.da...') #1 {main} thrown in C:\inetpub\wwwroot\db2.php on line 67

Any help would be appreciated.
0
Comment
Question by:Donnie Walker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38713850
You need to use AS to alias your tables.

                  FROM
                        WWW.dbo.Events e
                        LEFT OUTER JOIN WWW.dbo.Locations l

Should be:

                  FROM
                        WWW.dbo.Events AS e
                        LEFT OUTER JOIN WWW.dbo.Locations AS l
0
 

Author Comment

by:Donnie Walker
ID: 38713866
did that and got the same error.
0
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38713888
Try this:

$query = 'SELECT TOP e.date
                        ,e.startTime
                        ,e.endTime
                        ,e.title
                        ,e.details
                        ,l.name
                        ,e.room
                  FROM
                        WWW.dbo.Events AS e
                        LEFT OUTER JOIN WWW.dbo.Locations AS l
                              ON e.locationId = l.locationId
                  WHERE
                        WHERE e.date BETWEEN GETDATE() and DATEADD(month, 6, GETDATE())
                  ORDER BY
ORDER BY e.date ASC';

What does that return?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Donnie Walker
ID: 38713916
same error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'e'.' in C:\inetpub\wwwroot\db2.php:67 Stack trace: #0 C:\inetpub\wwwroot\db2.php(67): PDO->query('SELECT TOP e.da...') #1 {main} thrown in C:\inetpub\wwwroot\db2.php on line 67

if I do this:

$query = 'SELECT TOP 5 date ,startTime ,endTime ,title ,details ,room
FROM WWW.dbo.Events
WHERE date BETWEEN GETDATE() and DATEADD(month, 6, GETDATE())
ORDER BY date ASC';

without joining the second table I get results.

But I need the data from the second table as well.

I'm using "sqlsrv" and PDO if that makes a difference.
0
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38713939
Duplicate WHERE and ORDER BY statements...

What about this?

$query = 'SELECT TOP e.date
                        ,e.startTime
                        ,e.endTime
                        ,e.title
                        ,e.details
                        ,l.name
                        ,e.room
                  FROM
                        WWW.dbo.Events AS e
                        LEFT OUTER JOIN WWW.dbo.Locations AS l
                              ON e.locationId = l.locationId
                  WHERE e.date BETWEEN GETDATE() and DATEADD(month, 6, GETDATE())
                  ORDER BY e.date ASC';
0
 

Author Comment

by:Donnie Walker
ID: 38713946
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'e'.' in C:\inetpub\wwwroot\db2.php:73 Stack trace: #0 C:\inetpub\wwwroot\db2.php(73): PDO->query('SELECT TOP e.da...') #1 {main} thrown in C:\inetpub\wwwroot\db2.php on line 73
0
 
LVL 4

Accepted Solution

by:
brendonfeeley earned 2000 total points
ID: 38714034
No numeric value after the TOP statement. Try this:

$query = 'SELECT TOP 5 e.date, e.startTime, e.endTime, e.title, e.details, l.name, e.room
FROM WWW.dbo.Events AS e
LEFT OUTER JOIN WWW.dbo.Locations AS l
ON e.locationId = l.locationId
WHERE e.date BETWEEN GETDATE() and DATEADD(month, 6, GETDATE())
ORDER BY e.date ASC';
0
 

Author Closing Comment

by:Donnie Walker
ID: 38714045
duh! That worked. Thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question