php - android上传PHP数据如何与MySQL数据库连接?

标签 php android mysql

我创建了一个 Android 应用程序,用于将数据上传到我的网络服务器到 PHP 文件并将数据输出到 .txt 文件。我升级了服务器,现在支持数据库。

如何转换/更新以下 php 代码以连接 MySQL 数据库?在 android 端使用 HTTP POST 来 url mysite/location.php 我想添加记录纬度,经度到我的数据库...mysql查询...需要代码示例...

<?php
$lat = $_POST["lat"]; // Declares the upload data from client as a variable
$lon = $_POST["lon"];

$textfile = "location.txt"; // Declares the name and location of the .txt file

$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w   ') or die("Something went wrong!"); // Opens up the     .txt file for writing and replaces any previous content

$stringToWrite = " $lat\n $lon\n "; // Write location

fwrite($fh, $stringToWrite); // Writes it to the .txt file
fclose($fh);

//header("Location: index.html"); // Return to frontend (index.html)
?>

最佳答案

如果您希望能够在创建后管理和维护代码,您需要学习如何自己执行此操作。从 PHP 连接到 MySQL(在撰写本文时)最好使用 PDO 来完成。 .

编辑:

这里是使用 PDO 的示例代码的链接:

https://stackoverflow.com/a/4559320/1183321

关于php - android上传PHP数据如何与MySQL数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15818878/

相关文章:

python - 适用于 PostgreSQL 和 MySQL 的纯 python SQL 解决方案?

PHP + MySQL 导航 : How to load 2 levels in 1 query?

php - 如何从 zend 框架获取 $_GET 对象

php - Codeigniter 4级路由不起作用

android - 在Android中动态改变微调器的宽度

android - 从麦克风获取的语音音量任意降低(Android、OpenSLES)

javascript - 在 AngularJS 中处理缓存和更新的 JSON 文件

php - 在 php 中以 root 身份将上传的文件移动到不同的用户主文件夹

android - 登录行为中的 Activity LauchMode

php - Yii2 query all get value in join