简体   繁体   English

如何使用更新的mysql数据库信息更新javascript中的php变量?

[英]How do I update my php variables in javascript with updated mysql database information?

I have a database which is updating values, which are positions of an opponent in a multiplayer game. 我有一个更新值的数据库,这是多人游戏中对手的位置。

My problem occurs in trying to update the opponent models with their updated position information. 我的问题出现在试图用他们更新的位置信息更新对手模型。

I have two seperate php files to do this: 我有两个单独的PHP文件来执行此操作:

1.) update.php is called within the javascript file multiple times with 1.)update.php在javascript文件中多次调用

xmlhttp = new XMLHttpRequest(); xmlhttp = new XMLHttpRequest(); xmlhttp.open('GET', "xml_http_request.php?mod0=" + truckHeading + "&mod1=" + newhtr[1] + "&mod2=" + absRoll + "&lla0=" + lla[0] + "&lla1=" + lla[1] + "&lla2=" + lla[2] + "&pid=" + '1' + "&rangeCheck=" + rangeCheck + "&ranger=" + ranger + "&namely=" + namely + "&message=" + message + "&unLoader=false", true); xmlhttp.open('GET',“xml_http_request.php?mod0 =”+ truckHeading +“&mod1 =”+ newhtr [1] +“&mod2 =”+ absRoll +“&lla0 =”+ lla [0] +“&lla1 =” + lla [1] +“&lla2 =”+ lla [2] +“&pid =”+'1'+“&rangeCheck =”+ rangeCheck +“&ranger =”+ ranger +“&= =”+ ie +“&message =” + message +“&unLoader = false”,true); xmlhttp.send(null); xmlhttp.send(NULL);

2.) retrieve.php is only called once in my index.php file 2.)retrieve.php仅在我的index.php文件中调用一次

I believe the solution is to run the retrieve.php file once every time the update.php file is run (say right after the above code), but I don't know what the line of code is to do that. 我相信解决方案是每次运行update.php文件时运行一次retrieve.php文件(比如说在上面的代码之后),但我不知道代码行是做什么的。

Here's what's in my retrieve.php file: 这是我的retrieve.php文件中的内容:

require("db1.php"); //for using live public database

$query="SELECT mod0, mod1, mod2, lla0, lla1, lla2, namely, message FROM positioner WHERE id = 1 ";
$result=mysql_query($query);

while ($row=@mysql_fetch_assoc($result)){

    $mod0 = $row['mod0'];
    $mod1 = $row['mod1'];
    $mod2 = $row['mod2'];
    $lla0 = $row['lla0'];
    $lla1 = $row['lla1'];
    $lla2 = $row['lla2'];

}

echo "<script> var mod0php = $mod0; </script>";
echo "<script> var mod1php = $mod1; </script>";
echo "<script> var mod2php = $mod2; </script>";
echo "<script> var lla0php = $lla0; </script>";
echo "<script> var lla1php = $lla1; </script>";
echo "<script> var lla2php = $lla2; </script>";

?> ?>

If these are numbers, then I don't see a problem. 如果这些是数字,那么我没有看到问题。 but if they are strings, I see a problem at 但如果它们是字符串,我看到了一个问题

echo "<script> var mod0php = $mod0; </script>";
echo "<script> var mod1php = $mod1; </script>";
echo "<script> var mod2php = $mod2; </script>";
echo "<script> var lla0php = $lla0; </script>";
echo "<script> var lla1php = $lla1; </script>";
echo "<script> var lla2php = $lla2; </script>";

change this to 将此更改为

echo "<script> var mod0php = '$mod0'; </script>";
echo "<script> var mod1php = '$mod1'; </script>";
echo "<script> var mod2php = '$mod2'; </script>";
echo "<script> var lla0php = '$lla0'; </script>";
echo "<script> var lla1php = '$lla1'; </script>";
echo "<script> var lla2php = '$lla2'; </script>";

you are missing qoutes around the strings. 你缺少字符串周围的qoutes。

you would run it by 你会运行它

include 'retrieve.php';
or include_once 'retrieve.php';

however, this incurs a security risks. 但是,这会带来安全风险。 another method is to have php retrieve it using http. 另一种方法是让php使用http检索它。

$content = http_get("http://nowhere.com/retrieve.php"); //there are options on this call if you want

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM