Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 915
  • Last Modified:

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:
<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?>

Open in new window


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);
        }
}

Open in new window

0
DancingFighterG
Asked:
DancingFighterG
  • 9
  • 8
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Use two backticks or nothing to delimit the table name.  You had a single quote and a backtick which will not work.
$query = "SELECT * FROM `Users`";

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.
<?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");

Open in new window

0
 
DancingFighterGAuthor Commented:
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?>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DancingFighterGAuthor Commented:
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);
?>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
$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);
?>

Open in new window

0
 
DancingFighterGAuthor Commented:
I am still getting the same results with that php code
0
 
Dave BaldwinFixer of ProblemsCommented:
Show me exactly what you are getting.
0
 
DancingFighterGAuthor Commented:
This is what shows up in the Debug Log in Unity3d:
5.1.70-cll
UnityEngine.Debug:Log(Object)
<ConfirmLogin>c__Iterator0:MoveNext() (at Assets/Scripts/GameState.cs:112)
0
 
Dave BaldwinFixer of ProblemsCommented:
I don't care about the debug log.  Tell me what the PHP code displayed.
0
 
DancingFighterGAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
DancingFighterGAuthor Commented:
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
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
DancingFighterGAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
DancingFighterGAuthor Commented:
I get the same thing as my code.
8¿¿¿
5.1.70-cll¿Zêþ¿Bo,(%!sB¿ÿ÷0¿¿¿¿¿¿¿¿¿¿¿¿¿¿/a|n4S&a70(v¿¿¿ÿ„Got packets out of order
Would this possibly be cause by using the wrong port number?
0
 
Dave BaldwinFixer of ProblemsCommented:
We were leaving one ')' off the code on this line: while ($record = mysql_fetch_array($result))
<?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);
?>
                                            

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now