PDA

View Full Version : SQL query problem


fish
07-06-2002, 07:48 AM
Hi. This is just a simple SQL query but its really giving me pains.. i'd appreciate any ideas.


$query = "SELECT code FROM colours WHERE ('colour' = \"$colour\");";

$result = mysql_query($query) or die("Query failed");

$line = mysql_fetch_array($result, MYSQL_ASSOC);





When I execute this then run the print statements:


print "Value of line is: $line[code]";
$temp = $line[code];
print "temp is $temp";


the script simply prints out "Value of line is: " and same for temp.
I've pretty much isolated the problem to these lines, i've tested the other variables and inputs, so my problem is somewhere with the query :/

I appreciate any suggestions!

Strike
07-06-2002, 01:01 PM
$query = "SELECT code FROM colours WHERE colour='$colour'";

This will work.

fish
07-07-2002, 01:57 AM
Nope, still the same problem :/

Strike
07-07-2002, 02:56 AM
Oh, duh, you are trying to use the fetched array as an associative array, which it is not. The query may be fine (though that's weird syntax I've not seen).

$temp = $line[code];

Change that "code" to 0. Or, use mysql_fetch_assoc and then change that to read "code" (including quotes) instead of just code.

fish
07-11-2002, 04:13 AM
well I still couldn't get it working but I think i may have found the problem
when i use the query string:

[code]
$query = "SELECT code FROM colours WHERE colour='$colour'";
[\code]

it searches the database for ' red '
or ' black '

with the leading/trailing spaces, same thing as if you do
print " blah blah blah '$colour' blah blah"
it prints blah blah blah ' red ' blah blah"

is there a php built in function to remove leading/trailng spaces? or must i do it manually? :(

stuka
07-11-2002, 04:37 PM
From the PHP docs:

string trim (string str)


This function returns a string with whitespace stripped from the beginning and end of str. The whitespace characters it currently strips are: "\n", "\r", "\t", "\v", "\0", and a plain space.

roninblade
07-11-2002, 09:53 PM
it is always a good habit to to use trim() and mysql_escape_string() on your variables before using them in a query.

fish
07-12-2002, 12:42 AM
Thanks :)

stuka
07-12-2002, 11:14 AM
NP - btw, I seriously suggest downloading the PHP docs - they're in a separate d/l IIRC, but WELL worth any time pulling 'em down. It's got a GREAT function reference.