PHP如何从数据库行中获取数据?

时间:2014-12-11 20:24:00

标签: php android mysql json database

+---------+---------+--------+--------+
|  Col1   |  Col2   | Col3   |  Col4  |
+---------+---------+--------+--------+
| 1       | Value 1 |    a   |    A   |
+---------+---------+--------+--------+
| 2       | Value 2 |    b   |    B   |
+---------+---------+--------+--------+
| 3       | Value 3 |    c   |    C   |
+---------+---------+--------+--------+
| 4       | Value 4 |    d   |    D   |
+---------+---------+--------+--------+

假设这是数据存储在我的数据库表中的方式,我想要的是使用 PHP 获取此数据库表中的数据并存储在 JSON ,然后在我的 android 端解码JSON数据,并以类似格式显示它,就像在数据库表中一样,如第1行和第1行; (1,值1,a,A)然后对于第2行,第3行和第4行相同。请有人指导我如何做到这一点?

1 个答案:

答案 0 :(得分:1)

这是你的数据库到JSON的东西,至于与Android应用程序共享.... 如果您的应用程序可以查看该页面,它可以读取已经存在的JSON 回声

<?php

$dsn  = '';
$user = '';
$pass = '';

$dbh = New \PDO($dsn, $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare("SELECT * FROM your_database");
$sth->execute();

header('Content-Type: application/json');

/** is now json that displays to the screen */
echo json_encode($sth->fetchAll(PDO::FETCH_ASSOC));
exit;

至于Android部分,这样的事情应该有效

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ehime.readjsonfromurl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ehime.readjsonfromurl.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:textSize="18sp"
        android:text="@string/info"  />


    <TextView
        android:id="@+id/wid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="75dp"
        android:layout_marginTop="45dp"  />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:textStyle="bold"
        android:text="@string/wid" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wid"
        android:layout_marginLeft="75dp"
        android:layout_marginTop="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wid"
        android:layout_marginTop="30dp"
        android:text="@string/name"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/url"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginLeft="75dp"
        android:layout_marginTop="30dp" />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginTop="30dp"
        android:textStyle="bold"
        android:text="@string/url" />


</RelativeLayout>

的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Read Json From URL</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="info">Website Information from URL</string>
    <string name="col1">Column 1</string>
    <string name="col2">Column 2</string>
    <string name="col3">Column 3</string>

    .... etc ....

</resources>

MainActivity.java

package com.ehime.readjsonfromurl;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .penaltyDialog()
        .build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
        .penaltyLog()
        .build());


        TextView wid = (TextView) findViewById(R.id.wid);
        TextView name = (TextView) findViewById(R.id.name);
        TextView url = (TextView) findViewById(R.id.url);


        JSONObject json = null;
        String str = "";
        HttpResponse response;
        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://demos.ehimeofit.com/files/json.php");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try{
            JSONArray jArray = new JSONArray(str);
            json = jArray.getJSONObject(0);


            wid.setText(json.getString("id"));
            name.setText(json.getString("name"));
            url.setText(json.getString("url"));


        } catch ( JSONException e) {
            e.printStackTrace();               
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

上帝的速度