因此,我有一個遠程(託管)MySQL數據庫,我使用PHP服務連接到該數據庫。我需要我的ASP.Net c#web應用程序和我的android與它通信。但是,我正在用我從服務中檢索的所有信息填充我的Web應用程序模板。例如,我想填充用戶的配置文件頁面。從連接到遠程MySQL數據庫的PHP服務中檢索/獲取數據以顯示到我的ASP.Net 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();
}
}
此外,我不知道該從哪個函數返回。 請幫忙。
這個概念是以你可以使用的格式返回你需要的數據。這種格式,如果你正在創建對話者,可以隨心所欲。但是,有些方法大部分是標準化的。查找「JSON」。 – deg
無論您使用何種語言或您擁有多少應用程序都無關緊要。創建一個RESTful api並將其用作每個多應用程序中的數據訪問層。那麼所有這些應用程序都可以使用此api相互集成並傳遞數據。 – Valkyrie