一个PHP脚本应该有一个mysql_query?

时间:2015-05-08 07:28:27

标签: php mysql

所以我正在制作我的Android手机游戏服务器,db使用php,mysql。

当用户首次运行游戏时,我想将他的#include <cmath> #include <iostream> using namespace std; void toBinNum(int, int*, int&); int main(){ int* binNum = nullptr; //binNum will hold the array of binary digits int binNumSize; toBinNum(100, binNum, binNumSize); int numOnes = 0; for (int j = 0; j < binNumSize; j++){ if (binNum[j] == 1) //ERROR. numOnes++; cout << binNum[j]; } cout << numOnes; cin.get(); } /*void toBinNum(). Converts decimal number to binary. Parameters: binNum is an int pointer with value NULL, toBinNum() will use this pointer to store an int array of 0s and 1s it generates (each index will hold a binary value). binNumSize is an int, holds the length of the binNum array for use in other scopes decNum is an int, it is the decimal number that is to be converted to binary. */ void toBinNum(int decNum, int* binNum, int& binNumSize) { binNumSize = int(log2(decNum)) + 1; //How many digits will binNum be? binNum = new int[binNumSize]; int factor = pow(2, binNumSize - 1); //What is the largest 2^n that we should start dividing decNum by? //Find 1s and 0s for binary array for (int j = 0; factor >= 1; j++){ if (decNum / factor > 0){ binNum[j] = 1; // 1 1 0 0 1 decNum %= factor; //100 36 4 0 } else binNum[j] = 0; factor /= 2; // 64 32 16 8 4 2 } //Output each index of binNum. Confirmation that this function works. for (int j = 0; j < binNumSize; j++) cout << binNum[j] << endl; } 与我的游戏数据库进行比较,并检查他是ID还是new user

如果他是not,我想获取他从not new user获取的商品信息并将其发送给Unity客户并申请。

所以我正在制作PHP脚本,但问题是,

在这里,我需要执行2次db$sql。首先是比较mysql_query,其次是fetch ID

item info

然后,我可以将这两个进程输入到一个PHP脚本吗?在这种情况下,我怎样才能从统一中获取?回声是一个结果,不是吗?

还是应该分成2个PHP脚本?

1 个答案:

答案 0 :(得分:0)

请不要使用mysql_query。至少使用MySQLi,但(我认为)更好地使用PDO

回答你的问题:你可以在同一个PHP文件中创建两个SQL语句。实际上它很常见。您的代码看起来像这样:

$sql    = "select * from user";
$result = mysql_query($sql);
if (isset($result[0])) {
    $sql    = "select * from items where user_id = ?";
    $result = mysql_query($sql);
    // Parse result to unite all items in a readable format
    echo $result;
} else {
    echo 'registration succesful';
}

这只是示例代码,不会起作用,但它应该会让你朝着正确的方向前进。

编辑:
要将多种不同类型的数据返回到前端(Unity3D),建议捆绑所有数据。另外,以已知格式(例如JSON)格式化输出将使在前端解析数据变得更容易。

请考虑以下代码:

$response = array();
// Code to retrieve user in $user using msqli
if ($user) {
    // Retrieve items from user using mysqli
    $response['items']   = $items;
    $response['user']    = $user;
    $response['message'] = 'User authenticated';
} else {
    // Insert user into DB and save in $user using mysqli
    $response['user']    = $user;
    $response['message'] = 'User registered';
}

echo json_encode($response);