用于 C# 应用程序的 PHP 控制的 MySQL 数据库 [英] PHP controlled MySQL database for C# applications

查看:31
本文介绍了用于 C# 应用程序的 PHP 控制的 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个通过 PHP 连接到 MySQL 数据库的 C# 应用程序.我的 PHP 代码如下:

I am trying to make a C# application that connects to a MySQL database through PHP. My PHP code is the following:

<?php
require_once(dirname(__FILE__).'/dbconnect.php');

if (isset($_GET['name']) && isset($_GET['number']))
{
    //Get the POST variables
    $mName = $_GET['name'];
    $mNumber = $_GET['number'];

    //Insert new contact into database
    $sql = "INSERT INTO `products`(`name`) VALUES (\"" . $mName . "\")";

    echo $sql;

    //Execute query
    $stmt = mysqli_query($con, $sql); 
    if (!$stmt)
    {   
        echo 'Query Failed';     
    }
    else
    {
        echo "Query Successful";
    }
}

mysqli_close($con);
?>

在我的 C# 应用程序中,我使用 WebClient 和方法 DownloadString 来发出请求.

In my C# application I use a WebClient and the method DownloadString to make a request.

Uri uri = new Uri("http://manoest.dk/UpdateValue.php?name=John&number=23");

WebClient client = new WebClient();
client.Headers.Add("Content-Type", "text/html; charset=iso-8859-1");
//client.Headers.Add("Accept", "text/plain");
client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");


var streng = client.DownloadString(uri);

我尝试了很多不同的东西,但每次我都会收到 406 错误(不可接受).我猜标题是问题,但我试过了,看不到问题.

I have tried many different things, but everytime I will get an 406 error (Not acceptable). I guess that the headers are the problem, but I have tried and can't see the problem.

推荐答案

您需要在 PHP 文件中添加一个 header 调用以指示您正在生成的内容类型.例如,对于简单的文本:

You need to add a header call in the PHP file to indicate what content-type you're producing. For example, for simple text:

header('Content-Type: text/plain');

在生成任何输出之前添加此调用以避免出现问题.参见 http://php.net/manual/en/function.header.php

Add this call before producing any output to avoid problems. See http://php.net/manual/en/function.header.php

然后,在 C# 客户端中添加与 Accept 标头相同的内容类型.

Then, add the same content-type in your C# client as your Accept header.

这篇关于用于 C# 应用程序的 PHP 控制的 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆