Jump to content


Photo

Why doen't this simple query work?


  • Please log in to reply
3 replies to this topic

#1 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 02 March 2006 - 09:43 PM

Hello-Why doesn't this simple query work? Nothing prints. The variable, $item4, is a one-word text string submitted from a form. If I substitute an actual keyword in the query instead of the variable, it works.
Thank you.

<?
echo ("$item4"); //just testing to check the variable is correctly being sent from the form

$query = mysql_query("select make from table1 where keyword = '$item4' ");
$result = $query;
while ($row = mysql_fetch_array($result)){
print (" " . $row["make"] . " ");
}
?>

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 02 March 2006 - 09:57 PM

Try this to see if your query is actually returning anything, it might be that there are not any matches...
<?php
$result = mysql_query("SELECT `make` FROM `table1` WHERE `keyword`='$item4'");
if(mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        echo $row['make']."<br/>";
    }
} else {
    echo "There were no matches for the keyword you have supplied.";
}
?>


#3 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 02 March 2006 - 09:58 PM

G'day

Not to sure on the whole fetch and while part, I do it differnetly, (i should convert cause all you guys seem to do it like that though)

Have you checked case in the query and for the arrays? a lot of probs I get are mainly due to not having the right letter case everything seems very case sensitive when using PHP and MySQL on an apache server (as opposed to using MS softwares which I think most of us start with).
- Humpty
  #> Where does the light go when it goes out?

#4 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 02 March 2006 - 10:38 PM

Thanks for everyone's help. The problem is "operator error" on my part. When I coded the form some of the element values are preceded by a 'space', so they aren't matching and thus no result is returned. Duh!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users