Jump to content

[SOLVED] PHP SQL statement with variable not working, hardcoded is ok.. why?


Tjalfe

Recommended Posts

I am trying to make a small app which parses through an uploaded comma delimited text file and search a table to see what category each entry should be put in.

 

$Description=trim($LineItem[$ItemNumber-2]); 
//$Description="CDN TIRE STORE #00069 NEWMARKET ON";
   
$SQLSTRING="SELECT Category FROM DescriptionCategory WHERE Description = '$Description'";
echo"<TD>$SQLSTRING</TD>";
  
$result = mysql_query($SQLSTRING)or die ( "Error connecting to get 'SELECT * FROM Transactions WHERE Category = '$Category' order by 'Date' Ascending ' ");
    

 

if $Description=trim($LineItem[$ItemNumber-2]); is used, the SQL string looks 100% the same as when I use

$Description="CDN TIRE STORE #00069 NEWMARKET ON";

 

both end up like this, when viewed in firefox

 

SELECT Category FROM DescriptionCategory WHERE Description = 'CDN TIRE STORE #00069 NEWMARKET ON'

 

the interesting thing is that only the hard coded line works, the one where I get the description from the exploded text file does not.

What can I do?.. any suggestions? :)

 

thanks in advance

 

Tjalfe

 

 

Link to comment
Share on other sites

Are you getting a query error or simply zero matching rows?

 

The line in the file probably contains some non-printing characters, like nulls or \r\n (which don't show in rendered HTML in a browser, but would show up in the "view source" in the browser.) What does the "view source" in your browser show?

 

Try the following for the value you get from the file to see what the length actually is -

 

var_dump($Description);

Link to comment
Share on other sites

well, I guess I am feeling like a dummy for not trying the == test.. and printing them both, I now see in the page source why there is a problem ...

 

it seems that the data read from the file has more spaces in it..

<td>CDN TIRE STORE #00069 NEWMARKET    ON</td><td>CDN TIRE STORE #00069 NEWMARKET ON</td>

I guess I can blame firefox for removing spaces, as the hardcoded statement was pasted from firefox, and the data it was comparing with in the database was pasted the same way.. I guess I should go change the database to match the actual CSV file.

 

Thanks :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.