欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity与php关于mysql登录的实例解析

程序员文章站 2023-09-10 08:36:48
客户端代码: using unityengine; using unityengine.ui; using system.collections; public class log...

客户端代码:

using unityengine;
using unityengine.ui;
using system.collections;


public class login : monobehaviour
{
 public inputfield useridfield;
 public inputfield passwordfield;
 public text statustext;


 public button loginbtn;
 private string userid = "";
 private string password = "";
 private string url = "https://dasp.top/we/login.php";

 private void awake()
 {
  loginbtn.onclick.addlistener(onlogin);
 }
 public void onlogin()
 {
  userid = useridfield.text;
  password = passwordfield.text;

  if (string.isnullorempty(userid) || string.isnullorempty(password))
  {
print("账户和密码不能为空");

return;
  }

  startcoroutine(logining());
 }

 private ienumerator logining()
 {
  wwwform form = new wwwform();

  form.addfield("userid", userid);
  form.addfield("password", password);

  www www = new www(url, form);


  yield return www;

  if (www.error != null)
  {
print("error is login:" + www.error);
statustext.text = www.error + "...";
  }
  else
  {
print(www.text);
statustext.text = www.text;
  }

 }

}

在mysql建一个测试的数据表:

drop table if exists `tb1`;
create table `tb1` (
  `userid` varchar(30) not null,
  `password` varchar(50) not null,
  primary key (`userid`)
) engine=innodb default charset=latin1;

-- ----------------------------
-- records of tb1
-- ----------------------------
insert into `tb1` values ('100001', '123456');
insert into `tb1` values ('100002', '123456');
insert into `tb1` values ('100003', '123456');
insert into `tb1` values ('100004', '123456');

php端代码:

 

dbconfig.php
mysqli = mysqli_connect($this->host, $this->user, $this->pwd))
  {
die("cant connect into database");
  }
  else
  {

//echo  "连接数据库成功...
";
  }

  $this->select_db($this->db_name);
 }

 //析构函数
 function __destruct()
 {
  mysqli_close($this->mysqli);
 }

 /*
 * 说明:
 */
 public function get_mysql_handle()
 {
  return $this->mysqli;
 }

 /*
 * 说明:
 */
 public function select_db($_db)
 {
  if($this->mysqli != null)
  {
if(mysqli_select_db($this->mysqli, $_db))
{
 //echo  "连接数据库成功...
";
}
else
{
 die("cant connect into database");
}
  }
 }

 /*
 * 说明: 执行一个sql无返回值
 */
 public function execute($_sql)
 {
  if(empty($_sql))
  {
echo "参数不能为空";
return;
  }

  if(!mysqli_query($this->mysqli, $_sql))
  {

echo  "执行失败...
";
  }
 }

 /*
 * 说明: 执行一个查询语句,并执行回调函数
 */
 public function do_query($_sql, $query_callback = "")
 {
  if(empty($_sql))
  {
echo  "参数不能为空";

return;
  }

  if($result = mysqli_query($this->mysqli, $_sql))
  {
$num_rows = $result->num_rows;
if($num_rows > 0)
{
 while($row = $result->fetch_assoc())
 {
  if(!empty($query_callback))
  {
call_user_func( $query_callback , $row );
  }
 }

 return $num_rows;
}
else
{
 return 0;
}

mysqli_free_result($result);
  }
  else
  {
echo  "执行失败...
";
  }
 }


 //成员变量
 private $host = "localhost"; //数据库地址
 private $user = "root";//用户名
 private $pwd = ""; //用户密码
 private $db_name = "test";  //数据库
 private $mysqli = null;
}
?>

login.php

do_query($sql, "login_callback") > 0)
 {
  if($password_db == $password)
  {
//echo "登录成功...".$_post["userid"].",".$_post["password"].",".$password_db;
echo 1;
  }
  else
  {
//echo "登录失败1...".$_post["userid"].",".$_post["password"].",".$password_db;
echo 2;
  }
 }
 else
 {
  //echo "登录失败2...".$_post["userid"].",".$_post["password"].",".$password_db;
  echo "登录失败2...";
 }
}

function login_callback($row)
{
 global $password_db;
 $password_db = $row["v"];
}
?>