DancingFighterG
asked on
Issue with php not returning the database query to Unity3D
I am using WWW Forms in Unity3D to communicate with a php file on a webserver. The php file accesses a MySQL database on the same server and returns a query to Unity. The php file returns the the MySQL version number instead of the information from the query.
php file:
C# file:
php file:
<php
$databaseName;
$user;
$password;
$table;
$username;
$database = mysql_connect(localhost, $db_username, $db_password) or die ("Could not connect: ".mysql_error());
mysql_select_db($databaseName) or die("Could not select database");
$query = "SELECT * FROM 'Users`";
$result = mysql_query($query) or die('Query failed: '.mysql_error());
if(!isset($_POST['userName']) || !isset($_POST['PW']))
echo ("ERROR: FlashVars are absent;");
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $row['userName']."\t".$row['PW']."\n";
}
mysql_close($databaseName);
php?>
C# file:
IEnumerator function(string username, string pw)
{
string url = "XXX.X.XXX.XXX";
WWWForm form = new WWWForm();
form.addField("userName", username);
form.addField("PW", pw);
WWW www = new WWW(url, form);
yield return url;
if (login_get.error != null)
{
Debug.Log("There was an error connecting: " + login_get.error);
}
else
{
result = login_get.text;
Debug.Log(result);
}
}
ASKER
I apologize, I rushed a little bit posting the code, this is what the actual php file looks like. I still have the same issue
<php
$databaseName = "dbName";
$db_password = "password";
$db_username = "userName";
$database = mysql_connect(localhost, $db_username, $db_password) or die ("Could not connect: ".mysql_error());
mysql_select_db($databaseName) or die("Could not select database");
$query = "SELECT * FROM Users";
$result = mysql_query($query) or die('Query failed: '.mysql_error());
if(!isset($_POST['userName']) || !isset($_POST['PW']))
echo ("ERROR: FlashVars are absent;");
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $row['userName']."\t".$row['PW']."\n";
}
mysql_close($databaseName);
php?>
Your code doesn't make any sense and you didn't tell the exact error message. The line with $_POST['userName'] is not related to anything else in the code. The opening PHP tag should be "<?php" and the closing tag should be "?>". So if you are trying to actually run this code, nothing should work at all.
ASKER
I am very new to php so again I apologize. I agree that the line with $_POST['userName'] is not related to anything else in the code, I simply had it there to see if I could pull the variables from the WWWForm from the Unity3D application without any errors. I do not know the exact error that I am getting (if any). I just do not understand how to either send the information from the query back to the Unity3D application or why the only information I can pass from the php file is the MySQL version number from the webhosting service on the webserver.
<?php
$databaseName = "dbName";
$db_password = "password";
$db_username = "userName";
$database = mysql_connect(localhost, $db_username, $db_password) or die ("Could not connect: ".mysql_error());
mysql_select_db($databaseName) or die("Could not select database");
$query = "SELECT * FROM Users";
$result = mysql_query($query) or die('Query failed: '.mysql_error());
//if(!isset($_POST['userName']) || !isset($_POST['PW'])) echo ("ERROR: FlashVars are absent;");
$num_results = mysql_num_rows($result);
$result = mysql_fetch_array();
for($i = 0; $i < $num_results; $i++)
{
$record = mysql_fetch_array($result);
echo $record['userName']."\t".$record['PW']."\n";
}
echo $result;
mysql_close($database);
?>
$result returns a 'resource', not data. Try this.
<?php
$databaseName = "dbName";
$db_password = "password";
$db_username = "userName";
mysql_connect('localhost', $db_username, $db_password) or die ("Could not connect: ".mysql_error());
mysql_select_db($databaseName) or die("Could not select database");
$query = "SELECT * FROM Users";
$result = mysql_query($query) or die('Query failed: '.mysql_error());
while ($record = mysql_fetch_array($result)
{
echo $record['userName']."\t".$record['PW']."\n";
}
mysql_close($database);
?>
ASKER
I am still getting the same results with that php code
Show me exactly what you are getting.
ASKER
This is what shows up in the Debug Log in Unity3d:
5.1.70-cll
UnityEngine.Debug:Log(Obje ct)
<ConfirmLogin>c__Iterator0 :MoveNext( ) (at Assets/Scripts/GameState.c s:112)
5.1.70-cll
UnityEngine.Debug:Log(Obje
<ConfirmLogin>c__Iterator0
I don't care about the debug log. Tell me what the PHP code displayed.
ASKER
I do not know how to access that information. The file is run off the webserver and I don't know how to get access to the log files.
I didn't ask about log files. The PHP code you have posted echoes data to the screen and if you are running that page from your computer you should be able to see what it puts on the screen.
Who wrote the PHP code? The code you posted has turned out to be full of errors.
Who wrote the PHP code? The code you posted has turned out to be full of errors.
ASKER
The php file doesn't post anything to my screen because it is on a hosted webserver.
I ran the link to the php file in my browser and got this:
8¿¿¿
5.1.70-cll¿±œ4¿z}%?K<+`¿ÿ÷ 0¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿';7? x^OO|H.N¿ ¿¿ÿ„Got packets out of order
I ran the link to the php file in my browser and got this:
8¿¿¿
5.1.70-cll¿±œ4¿z}%?K<+`¿ÿ÷
Sorry but you are confused about what is supposed to happen. I have written dozens of PHP pages that access a MySQL databases and echo info to the screen. I know exactly how it works. If you directly access that PHP page, it should write out a list of usernames and passwords to the screen. It is not possible to get the response you show above.
I'll ask again, who wrote the PHP file? Is it you? Did you upload it to the 'hosted webserver'? Until you get this working, the "WWW Forms in Unity3D" don't even matter because the PHP isn't giving you a usable response.
I'll ask again, who wrote the PHP file? Is it you? Did you upload it to the 'hosted webserver'? Until you get this working, the "WWW Forms in Unity3D" don't even matter because the PHP isn't giving you a usable response.
ASKER
Yes I wrote the php file and uploaded it. I searched many pages on connecting Unity3D, php, and the MySQL database and this is what I based the php code off of.
Take the last version I posted above, edit it to have the proper values to access the database, and up load it to the server. Open your web browser, IE or Firefox, type in the complete URL for the PHP page, Not for Unity3D, and tell me exactly what you see on the screen. It Should be a list of usernames and passwords.
ASKER
I get the same thing as my code.
8¿¿¿
5.1.70-cll¿Zêþ¿Bo,(%!sB¿ÿ÷ 0¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿/a|n 4S&a70(v¿ ¿¿ÿ„Got packets out of order
Would this possibly be cause by using the wrong port number?
8¿¿¿
5.1.70-cll¿Zêþ¿Bo,(%!sB¿ÿ÷
Would this possibly be cause by using the wrong port number?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
The opening php tag is wrong above and the first 5 lines have no values assigned to them. And the next lines are not using the same variable names anyway. Your code should be generating errors.
Open in new window