0

I'm attempting to make a page that is just a CREATE VIEW using DreamWeaver CS5. How do I need to input my code to just receive a table with all data relevant to the logged in user.

mysql_query(
CREATE VIEW UserResults
AS 
   SELECT E.No, E. Description
      , Count(R.RID WHERE $Username= R.Owner)
      , Count(R.RID WHERE $Username= R.Owner AND Status==’Active’ )
   FROM ETable AS E, RTABLE as R
   ORDER BY E.No)
2
  • 1
    your sql is vulnerable to sql injection attacks fyi. Commented Dec 5, 2011 at 20:12
  • You also shouldn't generally be creating views from code -- just like tables, they stay around once they're created, so you should only have to create them once when your application is installed. Commented Dec 5, 2011 at 20:19

3 Answers 3

3

You have to put your query in quotes to make it a string.

mysql_query("CREATE VIEW UserResults AS SELECT E.No, E. Description, Count(R.RID WHERE $Username= R.Owner), Count(R.RID WHERE $Username= R.Owner AND Status==’Active’ ) FROM ETable AS E, RTABLE as R ORDER BY E.No")
Sign up to request clarification or add additional context in comments.

Comments

1

Daniel is correct about the quotes. However, you should probably take a look at PDO, the mysql_ functions are a bit outdated. You can read more about that here.

Comments

1
  • You have an extra ending parenthesis, which should be removed
  • Views should not have ORDER BY.
  • The WHERE clause should not be in the middle of the SELECT, you will need to use a CASE.
  • You are returning a Cartesian product instead of joining your tables together, this may provide more results than you want/expect.

The R.RID in the THEN block of the CASE statements may need to be a 1, not entirely sure what you hoped to get from those COUNT statements you originally had.

I tried to figure out what you were trying to do, and I think this is it:

CREATE VIEW UserResults AS
   SELECT E.No, E.Description
      , SUM(CASE 
               WHEN $Username = R.Owner THEN R.RID
               ELSE 0
            END) AS SumOfOwner
      , SUM(CASE
               WHEN $Username = R.Owner AND Status = 'Active' THEN R.RID
               ELSE 0
            END) AS SumOfOwnerAndActive
   FROM ETable AS E
   INNER JOIN RTABLE as R ON E.No = R.ENo
   GROUP BY E.No, E.Description

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.