Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    16

    Unanswered: using a php variable in the WHERE clause of a mysql query

    I'm running a very simple query that I think should work. The only thing that I haven't done before is put a php variable in the WHERE clause of the query. The variable $X is a numerical value, say 100. When I run this query, I just get a value of 0 returned. Am I doing something obviously stupid?

    SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n<= '$X'

    I've looked around the web and also tried this:

    SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n<= '" . $X . "'

    But this also just returns 0.

    Any ideas? Thanks in advance.

  2. #2
    Join Date
    Oct 2011
    Location
    Hamburg, Germany
    Posts
    18
    if n in your table is a number so you can try this one
    Code:
    'SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n<= '. $X

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    what datatype is $X?

    you can take advantage of soem of the quirks of PHP ferinstance the interprter will spot a $ is a valid variable

    PHP Code:
    $strSQL "SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n <= '
    $X';" 
    however that will be doing a string comparison (string values should be quoted so the SQL engine knows where they start and stop)

    for a numeric value don't 'quote' the variable

    PHP Code:
    $strSQL "SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n <= 
    $X;" 
    if not to certain as to how the PHP interpreter handles the decimal point '.'

    so it may be that you need to leave no spacfes betrween the decimal points
    PHP Code:
    $strSQL "SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n <= '"
    .$X."';" 
    again if generator_64k.n is a numeric value then don't use the quote marks as otherwise the SQL will interpret it as a string value.

    PHP Code:
    $strSQL 'SELECT generator_64k.n
    FROM generator_64k
    WHERE generator_64k.n <= '
    .$X.';' 
    the two last statements are probably better to use for a purist perspective (IE conform to more how code should be written as opposed to the mongrel approach available to you in PHP, but if it works ultimately who cares?
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Jul 2006
    Posts
    56
    when I query my oracle DB i have to do this: to_number($x). is the field you're querying of typer number? are there any records that fit the criteria you're querying?

  5. #5
    Join Date
    Mar 2012
    Posts
    16

    Cool Solved

    I actually got this to work by making sure the variable was declared before the sql statement in the actual code. Sometimes the easiest solutions...

Tags for this Thread

Posting Permissions

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