当前位置:   article > 正文

unity+MAMP(1):为游戏搭建数据库_如何给自己的游戏添加数据库

如何给自己的游戏添加数据库

链接:油管视频

下载MAMP之后,在web server的文件夹路径中加入配置文件:

login.php

<?php
$con = mysqli_connect('localhost','root','root','unitycpr');

	//check that connection happened
	if(mysqli_connect_errno())
	{
		echo "1: Connection failed";
		exit();
	}

	$username = $_POST["name"];
	$password = $_POST["password"];

	//check if name exits
	$namecheckquery = "SELECT username, salt,hash,score FROM players WHERE username='" . $username . "';";

	$namecheck = mysqli_query($con,$namecheckquery) or die("2: Name check query failed");

	if(mysqli_num_rows($namecheck) != 1)
	{
		echo "5: Either no user with name, or more than one";
		exit();
	}

	$existinginfo = mysqli_fetch_assoc($namecheck);
	$salt = $existinginfo["salt"];
	$hash = $existinginfo["hash"];

	$loginhash = crypt($password, $salt);
	if($hash != $loginhash)
	{
		echo "6: Incorrect password";
		exit();
	}

	echo "0\t" . $existinginfo["score"];


?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

register.php

<?php

	$con = mysqli_connect('localhost','root','root','unitycpr');

	//check that connection happened
	if(mysqli_connect_error())
	{
		echo "1: Connection failed";
		exit();
	}

	$username = $_POST["name"];
	$password = $_POST["password"];

	//check if name exits
	$namecheckquery = "SELECT username FROM players WHERE username='" . $username . "';";

	$namecheck = mysqli_query($con,$namecheckquery) or die("2: Name check query failed");

	if(mysqli_num_rows($namecheck)>0)
	{
		echo "3: Name already exists";
		exit();
	}

	//add user to the table
	$salt = "\$5\$rounds=5000\$" . "steamedhams" . $username . "\$";
	$hash = crypt($password, $salt);
	$insertuserquery = "INSERT INTO players (username, hash, salt) VALUES ('" . $username . "' , '" . $hash . "','" . $salt . "');";
	mysqli_query($con,$insertuserquery) or die("4: Insert player query failed");// error but don't know why

	echo("0");


?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

savedata.php

<?php
	$con = mysqli_connect('localhost','root','root','unitycpr');

	//check that connection happened
	if(mysqli_connect_errno())
	{
		echo "1: Connection failed";
		exit();
	}

	$username = $_POST["name"];
	$newscore = $_POST["score"];

	//double check only one
	$namecheckquery = "SELECT username FROM players WHERE username='" . $username . "';";

	$namecheck = mysqli_query($con, $namecheckquery) or die("2: Name check query failed");

	if(mysqli_num_rows($namecheck) != 1)
	{
		echo "5: Either no user with name, or more than one";
		exit();
	}

	$updatequery = "UPDATE players SET score = " . $newscore . " WHERE username = '" . $username . "';";
	mysqli_query($con,$updatequery) or die("7: Save query failed.");

	echo "0";


?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

webtest.php

<?php
	
	echo "123" . "\t" . 500;


?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/108922
推荐阅读
相关标签
  

闽ICP备14008679号