Solved

Why isn't this query working?

Posted on 2012-12-21
8
445 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CA single sign on 2 72
inserting while loop results 2 27
can windows 10 wamp send mail()? 3 32
Store image url and display it for user profile 6 23
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now