spaces in query string driving me nuts

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ecourt
Forum Newbie
Posts: 11
Joined: Mon Oct 13, 2003 9:53 pm

spaces in query string driving me nuts

Post 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!!
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Try this:

Code: Select all

<?php
...WHERE username='" . trim($uid) . "'";
?>
Last edited by McGruff on Wed Aug 10, 2005 2:40 pm, edited 1 time in total.
ecourt
Forum Newbie
Posts: 11
Joined: Mon Oct 13, 2003 9:53 pm

Post 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.
ecourt
Forum Newbie
Posts: 11
Joined: Mon Oct 13, 2003 9:53 pm

Post by ecourt »

WOO WOO!!

that trim thing worked -- thanks!!!

What exactly does it do?? :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Removes whitespace from the beginning and the end of a string.
Post Reply