【问题标题】:Do I have to create a separate php file for each CRUD method when connecting to Android?连接到 Android 时是否必须为每个 CRUD 方法创建一个单独的 php 文件?
【发布时间】:2013-06-13 04:34:50
【问题描述】:

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

【问题讨论】:

    标签: java php android mysql connect


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试

      $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/

      【讨论】:

        【解决方案3】:

        您可以使用 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();
        ?>
        

        【讨论】:

          猜你喜欢
          • 2019-11-30
          • 1970-01-01
          • 2011-11-16
          • 1970-01-01
          • 1970-01-01
          • 2012-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多