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!