Solved

Issue with php not returning the database query to Unity3D

Posted on 2013-07-01
17
809 Views
Last Modified: 2016-11-24
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
Comment
Question by:DancingFighterG
  • 9
  • 8
17 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39292386
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
 

Author Comment

by:DancingFighterG
ID: 39303449
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39303512
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
 

Author Comment

by:DancingFighterG
ID: 39304643
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39304656
$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
 

Author Comment

by:DancingFighterG
ID: 39304693
I am still getting the same results with that php code
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39304711
Show me exactly what you are getting.
0
 

Author Comment

by:DancingFighterG
ID: 39304733
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39304755
I don't care about the debug log.  Tell me what the PHP code displayed.
0
 

Author Comment

by:DancingFighterG
ID: 39304770
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39304792
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
 

Author Comment

by:DancingFighterG
ID: 39304797
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39304815
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
 

Author Comment

by:DancingFighterG
ID: 39304824
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39304832
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
 

Author Comment

by:DancingFighterG
ID: 39316459
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
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 39316507
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now