SQL query fails when adding a text field to the WHERE clause.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AaronL
    New Member
    • Jan 2007
    • 99

    SQL query fails when adding a text field to the WHERE clause.

    Hi guys,

    I just started to learn MySql and php and am relatively new to how databases work. I setup my database and wrote the following code and I get the error:
    Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource
    [code=php]
    mysql_select_db ("breedknowledg e", $con);

    $qheight = $_POST["height"];
    $qminwgt = $_POST["minwgt"];
    $qmaxwgt = $_POST["maxwgt"];
    $qexercize = $_POST["exercize"];
    $qgrooming = $_POST["grooming"];

    if ($qheight == "") { $qheight = "99"; }
    if ($qminwgt == "") { $qminwgt = "1"; }
    if ($qmaxwgt == "") { $qmaxwgt = "999"; }

    $result = mysql_query("SE LECT * FROM breeds WHERE weight >= $qminwgt AND weight <= $qmaxwgt AND exercize = $qexercize ORDER BY breedname");
    $numrows = mysql_num_rows( $result);
    [/code]
    Now when I remove "AND exercize = $qexercize" I don't get the error. The only difference is that this field is a text field. I can run the query just fine in my MySQL admin program through my hosting site. The code looks fine I don't get it! Please help! Thanks!
    Last edited by Atli; Jan 2 '09, 07:40 PM. Reason: Added [code] and [quote] tags.
  • AaronL
    New Member
    • Jan 2007
    • 99

    #2
    Nevermind I figured it out, I found that text variable references need to be delimited by a ' so I created a $sqlbuild variable and built the select code differently

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Did you try just adding the single-quotes?
      [code=php]
      mysql_query("SE LECT ... exercize = '$qexercize' ...");
      [/code]
      In any case, glad you found a solution.

      P.S.
      Please use &#91;code] tags when posting your code examples.
      (See How to ask a question)

      &#91;code] ... Code goes here... &#91;/code]

      P.P.S.
      I changed the title of the thread.
      Please refrain from using phrases like "need help" as the title. They are of no use at all.
      (See Use a good thread title)

      Thank you.
      Moderator

      Comment

      Working...