【问题标题】:Error: Connecting MySQL database in Android app using PHP错误:使用 PHP 在 Android 应用程序中连接 MySQL 数据库
【发布时间】:2015-06-03 11:48:39
【问题描述】:

我一直在尝试使用 php 从 android 应用程序连接 MySQL 数据库。我不知道我下面的代码有什么问题。谁能告诉我我需要做什么。

这是我的代码:

  1. sql查询

    CREATE TABLE user_detail
    (
    name varchar(30),
    age int(2),
    email varchar(30)
    );
    

2.getdata.php

<?php

$con=mysql_connect('localhost','root','');
mysql_select_db('hellodb',$con); 

$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];

$mysql_query("insert into user_detail(name,age,email) values('{$name}','{$age}','{$email}')");

?>

3.MainActivity.java

public class MainActivity extends ActionBarActivity {

    EditText eName,eAge,eEmail;
    Button inButton;
    InputStream is=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy tp = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(tp);
        setContentView(R.layout.main);

        eName= (EditText) findViewById(R.id.etname);
        eAge= (EditText) findViewById(R.id.etage);
        eEmail= (EditText) findViewById(R.id.etemail);
        inButton=(Button)findViewById(R.id.ibutton);

        inButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String name=""+eName.getText();
               String age=""+eAge.getText();
               String email=""+eEmail.getText();

                List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("name","name"));
                nameValuePairs.add(new BasicNameValuePair("age","age"));
                nameValuePairs.add(new BasicNameValuePair("email","email"));

                try {
                    HttpClient httpClient=new DefaultHttpClient();
                    HttpPost httpPost=new HttpPost("http://10.0.2.2:8888/demo/getdata.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response=httpClient.execute(httpPost);

                    HttpEntity entity=response.getEntity();

                    is=entity.getContent();

                } catch (ClientProtocolException e) {
                    Log.e("ClientProtocol","LogTag");
                    e.printStackTrace();
                }catch (IOException e) {
                    Log.e("LogTag","IOException");
                    e.printStackTrace();
                }
            }
        });
    }

}
  1. MandroidManifest.xml

  2. ma​​in.xml

    <EditText
        android:id="@+id/etname"
        android:hint="Enter Your Name"
        android:textAlignment="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    <EditText
        android:id="@+id/etage"
        android:hint="Enter Your Age"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/etemail"
        android:hint="Enter Your Email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    <Button
        android:layout_width="169dp"
        android:layout_height="wrap_content"
        android:text="Insert"
        android:id="@+id/ibutton"
        android:layout_gravity="center_horizontal" />
    

【问题讨论】:

  • 从 $mysql_query 中移除 $
  • 在您打开&lt;?php 标记error_reporting(E_ALL); ini_set('display_errors', 1); 后将错误报告添加到文件顶部,请stop using mysql_* functions。它们不再被维护并且是officially deprecated。改为了解prepared statements,并使用PDO
  • 是的,如果您使用的是最新的 php 版本,则不应使用 mysql 功能。让我发布答案。
  • android部分与这个问题无关。

标签: java php android mysql


【解决方案1】:

了解更多关于 PDO Here

$dbtype     = "mysql";
$dbhost     = "localhost";
$dbname     = "hellodb";
$dbuser     = "root";
$dbpass     = "";

$name  = $_POST['name'];
$age   = $_POST['age'];
$email = $_POST['email'];

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

$sql = "INSERT INTO user_detail(name,age,email) VALUES (:name,:age,:email)";

$q = $conn->prepare($sql);

$q->execute(array(':name'=>$name,
                  ':age'=>$age,
                  ':email'=>$email));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多