SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Query Help

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Baltimore
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query Help

    I am diong a query based on a username that is being passed over.
    here is what i have:

    sqltext = "select from sage where username = request.form(username)"

    i've tried variations of it and i keep getting error message. i have written so that it will pull everything the db, and that works. so i know that it has to be w/ this one portion.

    but i'm getting an error message.
    any help.

  2. #2
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have also written Request in quotes so ASP considers it as a part of string not an ASP variable. You should use it like this:

    Code:
    sqltext = "select from sage where username = '" & request.form(username) & "'"
    Note that I have joined a string then the variable and then also a string together and set it into the variable sqltest
    And note also the usage of single quote '
    If a variable in your SQL query is a string type variable then you should write it between single quotes. If it is an integer type then you shouldn't.

  3. #3
    SitePoint Member oldwebgeek's Avatar
    Join Date
    Mar 2003
    Location
    New York
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to use the LIKE clause for string comparisons:

    Code:
    sqltext = "select from sage where username like '" & request.form("username") & "';"
    Nolan Interactive: Web services and software for business
    Web Descant: Website Design and Marketing Blog
    No-Nonsense Hosting: Business Web hosting
    WebmailASP: Webmail client for ASP and IIS

  4. #4
    Guru Bullschmidt's Avatar
    Join Date
    Apr 2002
    Location
    USA
    Posts
    524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And along the same lines (but when a posted form field could be blank)...

    Classic ASP Design Tips - Search Criteria on Multiple Fields
    http://www.bullschmidt.com/devtip-searchcriteria.asp
    Example on a page receiving a post of a SQL statement based on two listboxes (Rep and Customer) that might each have a value of "" (i.e. blank) to show all with no criteria restrictions.

    ' Get posted form vars.
    Rep = Request.Form("Rep")
    Customer = Request.Form("Customer")

    ' Set strSQL.
    strSQL = "SELECT * FROM MyTable WHERE (1=1)"
    If Rep <> "" Then
    strSQL = strSQL & " AND (Rep='" & Rep & "')"
    End If
    If Customer <> "" Then
    strSQL = strSQL & " AND (Customer='" & Customer & "')"
    End If

    Response.Write "strSQLWhere: " & strSQLWhere

    And the (1=1) above is there as a placeholder (which doesn't affect the results since it is always true) as the SQL statement possibly may use AND with criteria after it or possibly the SQL statement may not have anything after it. For example a SQL statement without the (1=1) like this would not work: SELECT * FROM MyTable WHERE AND Customer='API'
    J. Paul Schmidt
    www.Bullschmidt.com - Freelance Web and Database Developer
    www.Bullschmidt.com/DevTip.asp - Classic ASP Design Tips

  5. #5
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)


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
  •