unity3d_webplayer_php connect MySQL database _3_php log in and read all data

Source: Internet
Author: User
Tags php server web hosting

1,php Login Code "login.php"

The complete code is as follows

<?PHP$name=$_post[' PName '];$password=$_post[' Ppwd '];if($name&&$password){$conn=mysql_connect("xxxxxxxx", "MySQL User name", "MySQL password") or die("Someconstrerror");mysql_select_db("Getfh_15188775_linmdb");mysql_query("Set Names UTF8");$sql= "SELECT * from ' Getfh_15188775_linmdb '. ' u_table ' WHERE pname = '$name' and ppwd= '$password‘"; $res=mysql_query($sql); $rows=mysql_num_rows($res);mysql_query($sql); if($rows){     Echo"login--okokok--"; }     Else{      Echo"error--1--"; }}Else { Echo"error--2--";}?>

Upload to the virtual host server, the code is very simple, do not understand their own Baidu PHP Foundation, a bunch of online

2,unity3d Login Code "Login.cs"

The code is as follows

usingUnityengine;usingSystem.Collections; Public classLogin:monobehaviour { Public stringServer_url =""; //Use this for initialization    voidStart () {Server_url="http://linm123.getfreehosting.co.uk/login.php"; }        //Update is called once per frame    voidUpdate () {} Public stringPname ="";  Public stringPpwd ="";  Public BOOLisclicked;  Public stringTempmessage="--login-xx--"; voidOngui () {Pname= GUI. TextField (NewRect ( the,0, -, -), Pname); Ppwd= GUI. TextField (NewRect ( the, -, -, -), ppwd); if(isclicked = =false) {            if(GUI. Button (NewRect ( the, -, -, -),"Login") ) {isclicked=true;            Startcoroutine (Registerdata ()); }} GUI. Label (NewRect ( the, the, Max, -), tempmessage);    } wwwform form;    WWW Logindownload; IEnumerator Registerdata () {form=NewWwwform (); Form. AddField ("pname", Pname); Form. AddField ("ppwd", PPWD); //Create a Download object//Create a Download objectLogindownload =NewWWW (Server_url, form); //Wait until the download is done//wait until the download is complete        yield returnLogindownload; Debug.Log ("Loginok--"+logindownload.text); Tempmessage=Logindownload.text; Isclicked=false; }}

Run Tests

Enter User name admin

Password 123

Click Sign In

The server gave us the corresponding login-okokok-, prove that the account password is correct

Let's look at the log in and read the data

3,php log in and iterate through all data codes in the table "logingetdata.php"

The complete code is as follows

<?PHP$name=$_post[' PName '];$password=$_post[' Ppwd '];if($name&&$password){$conn=mysql_connect("Xxxxmysql server Address", "Gmysql account", "MySQL password C") or die("Someconstrerror");mysql_select_db("Getfh_15188775_linmdb");mysql_query("Set Names UTF8");$sql= "SELECT * from ' Getfh_15188775_linmdb '. ' u_table ' WHERE pname = '$name' and ppwd= '$password‘"; $res=mysql_query($sql); $rows=mysql_num_rows($res);mysql_query($sql); if($rows){   /*echo "login--okokok--"; */$DBH= @mysql_connect("Xxxxmysql server Address", "Gmysql account", "MySQL password C");

if(!$DBH) { die("Error"); }
@mysql_select_db("Getfh_15188775_linmdb",$DBH);

$q= "SELECT * FROM U_table";

$rs=mysql_query($q,$DBH);

while($row=Mysql_fetch_row($rs)) {

Echo"name--:".$row[0]. " --pwd--: ".$row[1]. " \ r \ n---"; }

/*Ssssssend*/
}
Else

{ Echo"error--1--"; } }
Else { Echo"error--2--"; }

?>

The code principle is to first check the user name password is correct, if entered into the correct if statement, and then retrieve all the data in the database table, loop all the data returned

Upload the file to your web hosting PHP server

4,unity3d log in and read all data code "LoginGetAllData.cs"

usingUnityengine;usingSystem.Collections; Public classLogingetalldata:monobehaviour { Public stringServer_url =""; //Use this for initialization    voidStart () {Server_url="http://linm123.getfreehosting.co.uk/logingetdata.php"; }        //Update is called once per frame    voidUpdate () {} Public stringPname ="";  Public stringPpwd ="";  Public BOOLisclicked;  Public stringTempmessage="--getalldata-xx--"; voidOngui () {Pname= GUI. TextField (NewRect (310,0, -, -), Pname); Ppwd= GUI. TextField (NewRect (310, -, -, -), ppwd); if(isclicked = =false) {            if(GUI. Button (NewRect (310, -, -, -),"Loginalldata") ) {isclicked=true;            Startcoroutine (Registerdata ()); }} GUI. TextArea (NewRect ( -, -, the, $), tempmessage);    } wwwform form;    WWW Logindownload; IEnumerator Registerdata () {form=NewWwwform (); Form. AddField ("pname", Pname); Form. AddField ("ppwd", PPWD); //Create a Download object//Create a Download objectLogindownload =NewWWW (Server_url, form); //Wait until the download is done//wait until the download is complete        yield returnLogindownload; Debug.Log ("Getok--"+logindownload.text); Tempmessage=Logindownload.text; Isclicked=false; }}

Now let's test it.

Enter Account Admin

Password 123

Click Loginalldata

Successfully read out all the data

We release it into Webplayer, and running is no problem.

qq:2360450496

I am Lin Dream, love life, Love coding, love coffee!

Personal website: http://wai2dance.s1.jutuo.net/

Welcome to communicate with me technology, exchange experience!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.