2013-07-17 3 views
0

그래서 난 온라인 데이터베이스에서 검색 특정 분야에 필요한 HTTP 요청에의 NameValuePair에 추가.
매개 변수는 PHP 스크립트로 안드로이드 응용 프로그램에서 전달 될 수있다 (나는 하나 개의 매개 변수에 대한 작업 만하는 것이 여러 실패하는 것이 있습니다). PHP 스크립트일부 매개 변수를 기반으로, 좋아

<?php error_reporting (E_ALL^E_NOTICE); ?> 
<?php 
$username = "root"; 
$password = ""; 
$host = "localhost"; 
$dbname = "db.projectblue"; 

$chapter = mysql_real_escape_string($_POST['column']); 
$id = mysql_real_escape_string($_POST['id']); 

mysql_connect($host, $username, $password); 
mysql_select_db($dbname); 

$q=mysql_query("SELECT '$column' FROM tblphysics WHERE id='$id'"); 
while($e=mysql_fetch_assoc($q)) 
    $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 

?> 

당신은 SQL 쿼리 입력에 따라 동적으로 생성 할 수있다시피. $ column 변수를 버전이나 chapter와 같은 명확한 열 이름으로 바꾸면 작동하지만 변수는 사용하지 않습니다.

내 자바 코드

private JSONObject getJSONObject(int id, String column) { 
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
    nameValuePair.add(new BasicNameValuePair("column", column)); 
    nameValuePair.add(new BasicNameValuePair("id", Integer.toString(id))); 


    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(PARSER_URL); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } catch (Exception e) { 
     Log.e(LOG_TAG_JSON, "Error in http connection " + e.toString()); 
    } 
    // convert response to string 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     result = sb.toString(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error converting result " + e.toString()); 
    } 
    try { 
     JSONArray jArray = new JSONArray(result); 
     JSONObject json_data = jArray.getJSONObject(0); 

     return json_data; 

    } catch (JSONException e) { 
     Log.e(LOG_TAG_JSON, "Error parsing data " + e.toString()); 
    } 
    return null; 

} 

내가 그렇게

public String getChapter(int id) throws JSONException { 
    JSONObject object = getJSONObject(id, "chapter"); 
    String iChapter = object.optString("chapter"); 

    return iChapter; 
} 

내가 PHP 스크립트에서 두 변수이 공개 방법을 출력을 호출

(에서 로그와 같은 개인 메소드를 호출하고

Version (this is a column name in db) : 0 (should be 5) 
Chapter (this is a column name in db) : chapter (should be newton) 

내 질문은 :
왜 못해 다른 방법)이있다 하나 이상의 변수와 PHP 스크립트가 작동합니까?
내 NameValuePair 설정이 올바 릅니까?
는 심지어 동적 열 이름 및/또는 테이블 이름을 가진 SQL 쿼리를 만들 alowed 있습니까?

편집 : 나는 심지어 그것을 보지 못했다 수정은 매우 간단했다. 나는 $ chapter 대신 $ column을 가지며 쿼리의 변수는 "$ chapter"여야합니다.

+0

당신은 당신의 변수 클라이언트 또는 서버 쪽 중 하나를 untaint하지 않습니다를 취소 할 수 있습니다 전체 MySQL의 모듈은 PHP에서 더 이상 사용되지 않습니다. 난 당신이 원하는 사례의 수는 그렇게 할이 사이트에가있는 것을 달성하기 위해 PDO 또는 mysqli를 사용하십시오. – hd1

답변

관련 문제