Page 1 of 1

spaces in query string driving me nuts

Posted: Thu Oct 16, 2003 3:51 pm
by ecourt
ok -- I am pulling names in from a text file, and searching the database for them --

here is my query (you probably saw this earlier :))

$query="SELECT * FROM user WHERE username='$uid'";
print "query: $query <br>";
print "uid: $uid<br>";
$id = mysql_query($query,$db);
$user=mysql_fetch_array($id);
print $user[username];

I'm not getting any results -- if I sub a real name for the variable $uid, it works fine --- so I put all those print statments in there -- and it looks like there are spaces at the ends of the names I am pulling from the txt file.

is there a way to ignore them, or anyone got a good function to pull them off ??

Thanks again!!

Posted: Thu Oct 16, 2003 4:50 pm
by qads

Code: Select all

<?php
$query="SELECT * FROM user WHERE username='$uid'";
print "query: $query <br />"; 
print "uid: $uid<br />"; 
$id = mysql_query($query,$db)or die("sql error:<br />".mysql_error()); 
$user=mysql_fetch_array($id); 
print_r($user); 
?>
are you sure you have some/one row where username = $uid value?
post here if you get any errors.

Posted: Thu Oct 16, 2003 5:01 pm
by McGruff
Try this:

Code: Select all

<?php
...WHERE username='" . trim($uid) . "'";
?>

Posted: Thu Oct 16, 2003 5:05 pm
by ecourt
I changed the code, and got no errors ---

and I am SURE they match up -- only difference is there seems to be spaces at the end of the names in the text file.

I can take the $uid variable out, and manually put one of the names in, and it works fine.

Posted: Thu Oct 16, 2003 5:13 pm
by ecourt
WOO WOO!!

that trim thing worked -- thanks!!!

What exactly does it do?? :)

Posted: Thu Oct 16, 2003 5:54 pm
by JAM
Removes whitespace from the beginning and the end of a string.