0

我一直在网上查看有关使用 Android 连接 PHP 的示例,例如http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/。我见过的大多数示例都是这样的,它们只为一种数据库方法(如获取、读取或删除)创建一个 php 文件。因此,这是否意味着我必须为要查询的数据库表的每个不同的 CRUD 方法创建一个单独的 PHP 文件?有没有办法遵循通常的“模型”模型并在单个 PHP 文件中拥有所有 CRUD 方法?

4

3 回答 3

1

您最好的选择是使用 JSON 接口与您的 PHP/MySQL 服务器通信。如果您愿意,您可以轻松地将所有 CRUD 方法放在一个文件中。

这是入门指南: http: //andrewbrobinson.com/2011/01/29/building-an-android-app-with-php-json-backend-introduction/

于 2012-11-01T03:16:29.637 回答
0

尝试

$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

好读

http://www.coderzheaven.com/2011/07/27/android-phpmysql-connection-redone/

于 2012-11-01T03:16:17.550 回答
0

您可以使用 POST 或 GET 方法对同一个 PHP 文件执行不同的操作。

例如,在安卓上,

  1. 要获取所有表格,请访问 yoururl.com/?iwantto=select
  2. 要插入,您可以访问 yoururl.com/?iwantto=insert&data=asdf

在 php 中:

<?php
mysql_connect("host","username","password");
mysql_select_db("Deal");
$iwantto = $_GET['iwantto'];
if($iwantto=="select"){
    $sql=mysql_query("select * from CITY");
    while($row=mysql_fetch_assoc($sql))
        $output[]=$row;
    print(json_encode($output));
}
else if($iwantto=="insert")
{
    $data=$_GET['data'];
    mysql_query("delete from CITY where CITY_NAME like '".$data."'");
}
mysql_close();
?>
于 2012-11-01T03:31:56.107 回答