Reputation: 643
I have been slaving over this code for nearly 16 hours and the result still makes no sense to me at all.
What I am doing is making a Java Program that sends a POST parameter to a URL. I am also testing the same method using a form with Firefox.
The trouble is, on Firefox, I test the value that is being sent and echo it out. I also test the value against a value I know it should equal, but when Java sends the value, it returns false, but Firefox returns true, why is this?
PHP Code:
$player = array('code' => "code");
$msg = "neither";
if(isset($_POST['player']))
{
$player = getPlayer(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_TABLE_NAME, $_POST['player']);
$msg = "?player=" . $_POST['player'] . " post";
}
function getPlayer($DbHost, $DbUsername, $DbPassword, $DbName, $DbTableName, $name)
{
try
{
$dsn = "mysql:host=$DbHost;dbname=$DbName"; //Data Source Name = MySQL
$dbc = new PDO($dsn, $DbUsername, $DbPassword); //Connect to DB
$query = "SELECT * FROM $DbTableName WHERE name = :name";
$result = $dbc->prepare($query); //Prepare query
if($name == "pathurs")
{
echo 'true:' . $name;
}
else
{
echo 'false:' . $name;
}
$result->bindParam(':name', $name, PDO::PARAM_STR);
$result->execute(); //Execute Query
while($row = $result->fetchObject()) //Loop through results
{
$array = array();
//Convert stdClass($row) to array($array)
foreach($row as $key => $value)
{
$array[$key] = $value;
}
$dbc = null;
return $array;
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
echo "code:" . $player['code'] . $msg . $_POST['player'];
Java Code:
public String getContent(String url, String params)
{
String result = "";
try
{
this.url = new URL(url);
try
{
URLConnection con = this.url.openConnection();
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
ps.println(params);
InputStream is = con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = in.readLine()) != null)
{
result += line;
}
System.out.println(result);
}
catch(IOException e)
{
e.printStackTrace();
}
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
return result;
}
Firefox Output:
true:pathurscode:code?player=pathurs postpathurs
Java Program Output:
false:pathurscode:?player=pathurs postpathurs
Why is it when I compare $name == "pathurs"
(In PHP Code on line 20) it comes up true in Firefox but false in Java? It doesn't make sense to me seeing they're both tested in the PHP page and they are both strings, and they both output in the echo that they both equal a form of 'pathurs'
Is there something I've done wrong? I can't currently fathom a reason it would only happen when Java sends the information and not when Firefox does. I tested by replaced the PDO query with:
$query = "SELECT * FROM $DbTableName WHERE name = 'pathurs'";
and it works perfectly on both outputs. This makes me EVEN more confused. I hope someone can help!
Thanks in advance!
Upvotes: 1
Views: 186
Reputation: 5896
It's because you are using:
ps.println(params);
So it's sending a new line character after the params, ie: player=pathurs\n
Try:
ps.print(params);
Upvotes: 4
Reputation: 568
i don't see in your code where the comparison is taking place, but i can tell you this about java:
when comparing 2 strings in java, don't use ==, use the string class .equals method, or compareTo or something like that. the reason is, in java, two strings are only == if they refer back to the same object. Java will often do this on its own when you create strings, if you create a string called "name" and then elsewhere in a program create another string called "name" it'll look around and say "hey, he already made one called name, let's use a pointer to that" this can confuse people because it makes it so that == often is true, but it's unreliable.
so my guess would be that since one of the strings is being received from outside the program, they are not referring to the same string object, and therefore not ==.
if i misunderstood the question i apologize :)
Upvotes: -1