首页 \ 问答 \ 检索/获取数据以从连接到远程MySQL数据库的PHP服务显示到我的ASP.Net Web应用程序(Retrieve/fetch data to display to my ASP.Net web application from PHP service thats connected to remote MySQL database)

检索/获取数据以从连接到远程MySQL数据库的PHP服务显示到我的ASP.Net Web应用程序(Retrieve/fetch data to display to my ASP.Net web application from PHP service thats connected to remote MySQL database)

所以我有一个远程(托管)MySQL数据库,我用PHP服务连接到它。 我需要我的ASP.Net c#web应用程序和我的android与它进行通信。 但是,我正在努力用我从服务中检索的所有信息填充我的Web应用程序模板。 例如,我想填充用户的个人资料页面。

下面是我的PHP连接和与数据库的通信:

    `// Create connection
         $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM Vendor Where VendorId = 2"; //this is just a test
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo . $row["id"]. . $row["BusinessName"]. . $row["Location"]. . $row["Email"]. .$row["Website"]. .$row["ProductType"]. .$row["Contact"]. .$row["PaymentOption"]. .$row["ProfileImg"]."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
` 

然后(不共享我的所有设置)这将是asp.net c#与我的PHP文件/服务进行通信的代码示例。

public void getUserInfo(int id)
        {


            string BusinessName = lblBusiness.Text.Trim();
            string email = lblEmail.Text.Trim();
            string Contact = lblPhone.Text.Trim();

            string location = lblLocation.Text.Trim();
            string Website = lblWebsite.Text.Trim();

            string payment = lblPayment.Text.Trim();

            //Variables to get information from the service
            Stream dataStream = null;
            WebResponse response = null;
            StreamReader reader = null;
            //Stores the result from the server
            string responseFromServer = null;
            try
            {
                string requestMethod = "GET";
                //Sending this data across the stream
                string postData = "&Email=" + email +   "&BusinessName=" + BusinessName + "&Website=" + Website + "&PaymentOption=" + payment + "&Location=" + location + "&ProductType=" + ProductType + "&Contact=" + Contact + "";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                string URL = "";// url of php service location
                string contenttype = "application/x-www-form-urlencoded";
                //Create link to web service
                WebRequest request = WebRequest.Create(URL);
                //Pass the request method
                request.Method = requestMethod;
                request.ContentType = contenttype;
                request.ContentLength = byteArray.Length;
                dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                //Get response from the server
                response = request.GetResponse();
                dataStream = response.GetResponseStream();
                reader = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (dataStream != null && reader != null && response != null)
                {
                    dataStream.Close();
                    reader.Close();
                    response.Close();
                }
                //Getting the response from the service
                //string result = responseFromServer.ToString();

            }

        }

另外,我不知道从该功能返回什么。 请帮忙。


So I have a remote(hosted) MySQL database which I connect to with a PHP service. I need both my ASP.Net c# web application and my android to communicate with it. However, I'm struggling with populating my web application template with all the information I retrieve from the service. For instance, I would like to populate a profile page of the user.

Below would be my PHP connection and communication to the database:

    `// Create connection
         $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM Vendor Where VendorId = 2"; //this is just a test
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo . $row["id"]. . $row["BusinessName"]. . $row["Location"]. . $row["Email"]. .$row["Website"]. .$row["ProductType"]. .$row["Contact"]. .$row["PaymentOption"]. .$row["ProfileImg"]."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
` 

and then (without sharing all my setups) this would be the code sample for asp.net c# to communicate with my PHP file/service.

public void getUserInfo(int id)
        {


            string BusinessName = lblBusiness.Text.Trim();
            string email = lblEmail.Text.Trim();
            string Contact = lblPhone.Text.Trim();

            string location = lblLocation.Text.Trim();
            string Website = lblWebsite.Text.Trim();

            string payment = lblPayment.Text.Trim();

            //Variables to get information from the service
            Stream dataStream = null;
            WebResponse response = null;
            StreamReader reader = null;
            //Stores the result from the server
            string responseFromServer = null;
            try
            {
                string requestMethod = "GET";
                //Sending this data across the stream
                string postData = "&Email=" + email +   "&BusinessName=" + BusinessName + "&Website=" + Website + "&PaymentOption=" + payment + "&Location=" + location + "&ProductType=" + ProductType + "&Contact=" + Contact + "";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                string URL = "";// url of php service location
                string contenttype = "application/x-www-form-urlencoded";
                //Create link to web service
                WebRequest request = WebRequest.Create(URL);
                //Pass the request method
                request.Method = requestMethod;
                request.ContentType = contenttype;
                request.ContentLength = byteArray.Length;
                dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                //Get response from the server
                response = request.GetResponse();
                dataStream = response.GetResponseStream();
                reader = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (dataStream != null && reader != null && response != null)
                {
                    dataStream.Close();
                    reader.Close();
                    response.Close();
                }
                //Getting the response from the service
                //string result = responseFromServer.ToString();

            }

        }

also, Im not sure what to return from that function. Please help.


原文:https://stackoverflow.com/questions/46020779
更新时间:2023-07-22 17:07

最满意答案

为了知道玩家是否运行命令,你必须将玩家的UUID存储在某个地方。 首先,你创建一个Set<UUID> ,它暂时存储已执行命令的所有玩家的所有唯一ID,所以当你看到存储在这个集合中的玩家时,你知道他们执行了命令。 UUID是一个36个字符的字符串,对每个播放器都是唯一的,并且在每个服务器上都是相同的。 你像这样制作Set

final Set<UUID> players = new HashSet<>();

接下来你需要做出命令。 我会这样做:

@Override
public boolean onCommand(CommandSender sender, Command cmd, String cl, String[] args) {
    //Check if your command was executed
    if(cmd.getName().equalsIgnorecase("yourCommand")){
        //Check if the executor of the command is a player and not a commandblock or console
        if(sender instanceof Player){

            Player player = (Player) sender;

            //Add the player's unique ID to the set
            players.add(player.getUniqueId());
        } 
    }
}

现在你接下来要做的是监听PlayerInteractEvent以查看玩家何时点击该书。 如果您看到播放器在Set ,则表示他们已执行该命令。 以下是我将如何制作EventHandler

@EventHandler
public void onInteract(PlayerInteractEvent event){
    //Check if the player right clicked.
    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
        //Check if the Set contains this player
        if(players.contains(event.getPlayer().getUniqueId()){
            //Check if the player had an item in their hand
            if(event.getPlayer().getItemInHand().getType() == Material.BOOK){
                //Remove player from the set so they have to execute the command again before right clicking the book again
                players.remove(event.getPlayer().getUniqueId());
                //Here you can do whatever you want to do when the player executed the command and right clicks a book.
            }
        }
    }
}

所以我所做的就是当玩家执行命令时,将它们存储在一个Set 。 接下来侦听PlayerInteractEvent 。 这基本上是每次玩家交互时调用的回调方法。 这可能是当玩家步进压力板,当玩家右手或左手点击一个块或空中等时。

在那个PlayerInteractEvent ,我检查玩家是否存储在该Set ,如果玩家右键单击一个块或右键单击一个块并检查玩家手中是否有一本书。 如果这一切都正确,我从Set删除播放器,这样他们就必须再次执行命令才能执行相同的操作。

另外,不要忘记注册事件并实现Listener

如果您想了解更多关于Set ,可以在这里找到Javadocs。


In order to know if the player ran the command, you have to store the player's UUID somewhere. First of all you create a Set<UUID> which temporarily stores all the Unique ID's of all players that have executed the command, so when you see a player stored in this set, you know they executed the command. A UUID is a 36-character string that is unique for every player and is the same on every server. You make the Set like this:

final Set<UUID> players = new HashSet<>();

Next you need to make your command. I would do that like this:

@Override
public boolean onCommand(CommandSender sender, Command cmd, String cl, String[] args) {
    //Check if your command was executed
    if(cmd.getName().equalsIgnorecase("yourCommand")){
        //Check if the executor of the command is a player and not a commandblock or console
        if(sender instanceof Player){

            Player player = (Player) sender;

            //Add the player's unique ID to the set
            players.add(player.getUniqueId());
        } 
    }
}

Now what you do next is listen for the PlayerInteractEvent to see when a player clicks the book. If you see the player is in the Set, you know they have executed the command. Here is how I would make that EventHandler:

@EventHandler
public void onInteract(PlayerInteractEvent event){
    //Check if the player right clicked.
    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
        //Check if the Set contains this player
        if(players.contains(event.getPlayer().getUniqueId()){
            //Check if the player had an item in their hand
            if(event.getPlayer().getItemInHand().getType() == Material.BOOK){
                //Remove player from the set so they have to execute the command again before right clicking the book again
                players.remove(event.getPlayer().getUniqueId());
                //Here you can do whatever you want to do when the player executed the command and right clicks a book.
            }
        }
    }
}

So what I've done is when the player executes the command, store them in a Set. Next listen for the PlayerInteractEvent. This is basicly a callback method called every time a player interacts. This could be when a player steps a pressureplate, when a player right or left clicks on a block or in the air etc.

In that PlayerInteractEvent, I check if the player is stored in that Set, if the player right clicked in the air or right clicked a block and check if the player has a book in their hand. If that's all correct, I remove the player from the Set so they have to execute the command once again to perform the same action.

Also don't forget to register events and implement Listener.

In case you wanna learn more about the Set, the Javadocs can be found here.

相关问答

更多
  • 问题来自这条线: EventTrigger类期望路由事件使用RoutedEventHandler委托而不是EventHandler委托。 这些是您必须在代码中进行的更改才能使其工作: 在ExtendedDataGrid : public class ExtendedDataGrid : DataGrid { public static ...
  • 添加新单元格时,向它们添加一个设置其样式的事件侦听器。 Address Book