Connecting Tech Pros Worldwide Help | Site Map

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

chelf's Avatar
Member
 
Join Date: Jan 2007
Posts: 54
#1: Jan 1 '09
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:
Quote:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Expand|Select|Wrap|Line Numbers
  1. mysql_select_db("breedknowledge", $con);
  2.  
  3. $qheight = $_POST["height"];
  4. $qminwgt = $_POST["minwgt"];
  5. $qmaxwgt = $_POST["maxwgt"];
  6. $qexercize = $_POST["exercize"];
  7. $qgrooming = $_POST["grooming"];
  8.  
  9. if ($qheight == "") { $qheight = "99"; }
  10. if ($qminwgt == "") { $qminwgt = "1"; }
  11. if ($qmaxwgt == "") { $qmaxwgt = "999"; }
  12.  
  13. $result = mysql_query("SELECT * FROM breeds WHERE weight >= $qminwgt AND weight <= $qmaxwgt AND exercize = $qexercize ORDER BY breedname");
  14. $numrows = mysql_num_rows($result);
  15.  
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!
chelf's Avatar
Member
 
Join Date: Jan 2007
Posts: 54
#2: Jan 1 '09

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


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
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,745
#3: Jan 2 '09

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


Hi.

Did you try just adding the single-quotes?
Expand|Select|Wrap|Line Numbers
  1. mysql_query("SELECT ... exercize = '$qexercize' ...");
  2.  
In any case, glad you found a solution.

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

[code] ... Code goes here... [/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
Reply


Similar MySQL Database bytes