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)
Adam Wenger
  • 17,100
  • 6
  • 52
  • 63
MadMoney
  • 31
  • 1
  • 4
  • 1
    your sql is vulnerable to sql injection attacks fyi. – Daniel A. White Dec 05 '11 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. –  Dec 05 '11 at 20:19

3 Answers3

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")
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
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.

Community
  • 1
  • 1
SuperTron
  • 4,203
  • 6
  • 35
  • 62
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
Adam Wenger
  • 17,100
  • 6
  • 52
  • 63