SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: mysql query

  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question mysql query

    i want to add the following where clause in the query
    PHP Code:
    WHERE username='".addslashes($username)."' 
    how do i do it?
    PHP Code:
    $result mysql_query"SELECT c.category_id,c.category_name,COUNT(photo_id)
                            FROM gallery_category as c
                            LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
                            GROUP BY c.category_id "
    ); 

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which table contain username field?

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    table gallery_category

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    make sure you always qualify every column used in a query with more than one table

    so it's COUNT(p.photo_id) and WHERE c.username = ...

    the WHERE clause goes right after the FROM clause (which includes all joins) and just before the GROUP BY clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    its not working
    i changed the coding to
    PHP Code:
    $username=$_SESSION["username"];
            
    $result mysql_query"SELECT c.category_id,c.category_name,COUNT(photo_id)
                            WHERE c.username='"
    .addslashes($username)."'
                            FROM gallery_category as c
                            LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
                            GROUP BY c.category_id"
    ); 

  6. #6
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    thanks...it worked....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •